出现错误时捕获图像 [英] Get Error when capture image
本文介绍了出现错误时捕获图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用相机捕捉到的图像,并希望映像路径。
我尝试以下code,但我得到的错误。 (我请点击此链接<一个href="http://stackoverflow.com/questions/4184951/get-path-of-image-from-action-image-capture-intent">Get图像从ACTION_IMAGE_CAPTURE意图路径)
下面是我的code。
MainActivity.java
公共类MainActivity延伸活动{
私有静态最终诠释CAMERA_REQUEST = 1888;
私人ImageView的ImageView的;
私人乌里mCapturedImageURI;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
this.imageView =(ImageView的)this.findViewById(R.id.imageView1);
按钮photoButton =(按钮)this.findViewById(R.id.button1);
photoButton.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
字符串文件名=temp.jpg;
ContentValues值=新ContentValues();
values.put(MediaStore.Images.Media.TITLE,文件名);
mCapturedImageURI = getContentResolver()插入(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,值)。
意向意图=新的意图(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,mCapturedImageURI);
startActivityForResult(意向,CAMERA_REQUEST);
}
});
}
保护无效onActivityResult(INT申请code,INT结果code,意图数据){
如果(要求code == CAMERA_REQUEST和放大器;&安培;结果code == RESULT_OK){
的String []投影= {MediaStore.Images.Media.DATA};
光标光标= managedQuery(mCapturedImageURI,投影,NULL,NULL,NULL);
INT column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
字符串capturedImageFilePath = cursor.getString(column_index_data);
Toast.makeText(getApplicationContext(),capturedImageFilePath.toString(),Toast.LENGTH_LONG).show();
}
}
}
activity_main.xml
&LT; XML版本=1.0编码=UTF-8&GT?;
&LT; LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=垂直&GT;
&LT;按钮
机器人:ID =@ + ID /按钮1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=照片&GT;
&LT; /按钮&GT;
&LT; ImageView的
机器人:ID =@ + ID / imageView1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:SRC =@可绘制/ ic_launcher&GT;
&LT; / ImageView的&GT;
&LT; / LinearLayout中&GT;
AndroidManifest.xml中
&LT;使用特征的android:NAME =android.hardware.camera&GT;&LT; /使用特征&GT;
&LT;使用-权限的Android:名称=android.permission.WRITE_EXTERNAL_STORAGE/&GT;
错误
了java.lang.RuntimeException:无法恢复活动{com.example.cameraupload / com.example.cameraupload.MainActivity}:java.lang.RuntimeException的:不提供结果ResultInfo {谁= NULL,请求= 1888,结果= -1,数据= NULL}到活动{com.example.cameraupload / com.example.cameraupload.MainActivity}:显示java.lang.NullPointerException
解决方案
最后,我得到了答案。
photoButton.setOnClickListener(新OnClickListener(){
公共无效的onClick(视图v){
档案文件=新的文件(Environment.getExternalStorageDirectory()+/ test1.jpg);
乌里outputFileUri = Uri.fromFile(文件);
意向意图=新的意图(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,outputFileUri);
startActivityForResult(意向,0);
}
});
保护无效onActivityResult(INT申请code,INT结果code,意图数据){
Log.i(MakeMachine,请求code:+请求code +,因此code:+导致code);
档案文件=新的文件(Environment.getExternalStorageDirectory()+/ test1.jpg);
如果(file.exists()){
BitmapFactory.Options选项=新BitmapFactory.Options();
options.inSampleSize = 4;
picFileName = Environment.getExternalStorageDirectory()+/ test.jpg放在;
Toast.makeText(getApplicationContext(),picFileName,Toast.LENGTH_LONG).show();
点阵位图= BitmapFactory.de codeFILE(picFileName,期权);
imageView.setImageBitmap(位);
//imageView.setVisibility(View.VISIBLE);
}
}
I want to capture image using camera and want image path.
I try following code but I get Error. (I follow this link Get Path of image from ACTION_IMAGE_CAPTURE intent)
Here is my code.
MainActivity.java
public class MainActivity extends Activity {
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;
private Uri mCapturedImageURI;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.imageView = (ImageView)this.findViewById(R.id.imageView1);
Button photoButton = (Button) this.findViewById(R.id.button1);
photoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String fileName = "temp.jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
mCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
startActivityForResult(intent, CAMERA_REQUEST);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
String[] projection = { MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(mCapturedImageURI, projection, null, null, null);
int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String capturedImageFilePath = cursor.getString(column_index_data);
Toast.makeText(getApplicationContext(), capturedImageFilePath.toString(), Toast.LENGTH_LONG).show();
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="photo" >
</Button>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
</LinearLayout>
AndroidManifest.xml
<uses-feature android:name="android.hardware.camera"></uses-feature>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Error
java.lang.RuntimeException: Unable to resume activity {com.example.cameraupload/com.example.cameraupload.MainActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1888, result=-1, data=null} to activity {com.example.cameraupload/com.example.cameraupload.MainActivity}: java.lang.NullPointerException
解决方案
Finally I got Ans.
photoButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
File file = new File(Environment.getExternalStorageDirectory()+"/test1.jpg");
Uri outputFileUri = Uri.fromFile(file);
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, 0);
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i("MakeMachine", "requestCode:"+requestCode + ",resultCode: " + resultCode);
File file = new File(Environment.getExternalStorageDirectory()+"/test1.jpg");
if(file.exists()){
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
picFileName=Environment.getExternalStorageDirectory()+"/test.jpg";
Toast.makeText(getApplicationContext(), picFileName, Toast.LENGTH_LONG).show();
Bitmap bitmap = BitmapFactory.decodeFile(picFileName, options);
imageView.setImageBitmap(bitmap);
//imageView.setVisibility(View.VISIBLE);
}
}
这篇关于出现错误时捕获图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文