java - 如何处理从相机保存图像的错误?
问题描述
我正在尝试制作一个带有相机的应用程序。点击图片后,您可以将图片保存在手机中。我看过几个教程并相应地编写了代码(我是 Android 工作室的新手)。然而,当其他人没有得到它们时,我得到了语法错误。我有两个活动。下面是我的 MainActivity.java:
package com.none.www.dumpcam;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainActivity extends AppCompatActivity {
String currentImagePath = null;
private ImageView mimageView;
private static final int IMAGE_REQUEST = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void captureImage(View view){
Intent imageTakeIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(imageTakeIntent.resolveActivity(getPackageManager())!=null){
File imageFile = null;
try {
imageFile = getImageFile();
} catch (IOException e){
e.printStackTrace();
}
if(imageFile!=null){
Uri imageUri = FileProvider.getUriForFile(context:this, authority:"com.example.android.fileprovider", imageFile); //one error is shown here
imageTakeIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(imageTakeIntent, IMAGE_REQUEST);
}
}
}
public void displayImage(View view){
Intent intent = new Intent(this,DisplayImage.class);
intent.putExtra(name: "image_path", currentImagePath);
startActivity(intent);
}
private file getImageFile() throws IOException{
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageName = "jpg_"+timeStamp+"_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File imageFile = File.createTempFile(imageName, suffix ".jpg", storageDir);
currentImagePath = imageFile.getAbsolutePath();
return imageFile;
}
}
以下是我的 DisplayImage.java:
package com.none.www.dumpcam;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
public class DisplayImage extends AppCompatActivity {
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_image);
imageView = findViewById(R.id.mimageView);
Bitmap bitmap = BitmapFactory.decodeFile(getIntent().getStringExtra( name: "image_path")); //One error is shown here.
imageView.setImageBitmap(bitmap);
}
}
就像我说的那样,我正在编写教程中的代码。那里的代码工作正常。我收到以下语法错误:
com/none/www/dumpcam/DisplayImage.java
error: ')' expected
error: ';' expected
error: illegal start of expression
error: ';' expected
com/none/www/dumpcam/MainActivity.java
error: ')' expected
error: not a statement
error: ';' expected
error: not a statement
error: ';' expected
error: not a statement
error: ';' expected
error: not a statement
error: not a statement
error: ')' expected
error: ';' expected
解决方案
推荐阅读
- java - 将类方法组织成专门的子类是一种好习惯吗?
- apache-spark - 无法在 Maven 中导入 spark SQL
- java - 如何通过 id 获取其他用户信息(用户名、名字)?[钥匙斗篷]
- sql - 使用追加模型在 BigQuery 中进行部分行更新
- java - 为什么 netty FastThreadLocal public final V get(InternalThreadLocalMap threadLocalMap) 不调用 registerCleaner 方法?
- android - 如何在 android 中使用开放街道地图包含 GeoJson?
- python - Pandas - 检查其他列是否有基于不同列的重复项
- mysql - 在字符串表mysql语句中查找数字
- python - 是否有可能为 Fiddler 或 mitmdump 中的每个请求动态更改 IP?
- c++ - 在“consoleHandler”功能从用户获取 Ctrl-C 后无法立即获取下一个用户命令