[英]How to Capture Image in Fragment Android
I am working with capturing an image and then show it on my ImageView in my fragment. 我正在捕获图像,然后将其显示在片段中的ImageView上。 I searched this through web and still I cannot make it. 我通过网络搜索了此内容,但仍然无法实现。 Below is what I am doing. 下面是我在做什么。
btnCaptureImg.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 1888);
}
});
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.e("LOG", ""+requestCode);
if (requestCode == 1888 && resultCode == Activity.RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
super.onActivityResult(requestCode, resultCode, data);
}
The Logcat says 1888 only but the imageView did not load the image. Logcat仅说1888,但是imageView不会加载图像。
I also tried this 我也尝试过
btnCaptureImg.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
File folder = new File(Environment.getExternalStorageDirectory().toString()+"/ImagesFolder/");
folder.mkdirs();
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
resultingFile = new File(folder.toString() + "/image.jpg");
Uri uriSavedImage=Uri.fromFile(resultingFile);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(cameraIntent, 1888);
}
});
But it throws exception: 但这会引发异常:
E/AndroidRuntime(4824): java.lang.RuntimeException: Unable to resume activity {com.myapp.android/com.myapp.android.MyHomeActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=264032, result=-1, data=null} to activity {com.myapp.android/com.myapp.android.MyHomeActivity}: java.lang.NullPointerException
E/AndroidRuntime(4824): at com.myapp.android.MyFragment.onActivityResult(MyFragment.java:300)
Check if your onActicityResult
is beeing called. 检查您的onActicityResult
是否被调用。 If not check: onActivityResult is not being called in Fragment 如果不是,请检查: 片段中未调用onActivityResult
Try putting 试穿
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, your_image_uri);
before this 在这之前
startActivityForResult(cameraIntent, 1888);
I'm sorry mate but I can't seem to understand why it's giving that exception. 对不起,队友,但我似乎无法理解为什么会出现这种异常。 Try this approach, it works for me. 试试这种方法,对我有用。
Define the file globally and inside onActivityResult 全局定义文件,并在onActivityResult内部定义
if (requestCode == 1888 && resultCode == Activity.RESULT_OK) {
Bitmap photo = Media.getBitmap(getActivity().getContentResolver(), Uri.fromFile(resultingFile));
//Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
First, in the fragment starts the intent to capture the image: 首先,在片段中开始捕获图像的意图:
private void startIntentImageCapture() {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
try {
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = File.createTempFile("imageName.jpg", ".jpg", storageDir);
path = Uri.fromFile(image);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, path);
getActivity().startActivityForResult(cameraIntent, CAPTURE_IMAGE_REQUEST_CODE);
} catch (IOException e) {
e.printStackTrace();
}
}
path
variable, must be an public static Uri
instance. path
变量,必须是公共静态Uri
实例。 And another public static Integer
for the request code: public static final Integer CAPTURE_IMAGE_REQUEST_CODE = 100;
另一个公共静态Integer
作为请求代码: public static final Integer CAPTURE_IMAGE_REQUEST_CODE = 100;
Then in the Activity
, read this path and decode the image: 然后在Activity
,读取此路径并解码图像:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == YourFragment.CAPTURE_IMAGE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Bitmap imageBitmap = decodeFile(YourFragment.path);
//do something with your photo
}
}
}
I'm sure this is not the best and cleaner method but it works for me. 我敢肯定这不是最好和更清洁的方法,但是对我有用。 Hope it helps! 希望能帮助到你!
EDIT 编辑
I forgot the code to decode the image. 我忘记了解码图像的代码。
public Bitmap decodeFile(Uri uriFile) throws OutOfMemoryError {
String filePath = uriFile.getPath();
BitmapFactory.Options bmOptions;
Bitmap imageBitmap;
try {
imageBitmap = BitmapFactory.decodeFile(filePath);
} catch (OutOfMemoryError e) {
bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 4;
bmOptions.inPurgeable = true;
imageBitmap = BitmapFactory.decodeFile(filePath, bmOptions);
}
return imageBitmap;
}
The bmOptions
ensure that OutOfMemoryError doesn't occurs. bmOptions
确保不会发生OutOfMemoryError。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.