我正在实施相机应用程序。我从相机捕获图像并将其保存到DCIM文件夹中并将图像显示到ImageView中。图像成功存储到DCIM文件夹中,但不会在全屏尺寸的ImageView中显示。而且我的应用程序崩溃并且出现错误
Uri selectedImageUri = data.getData();是空对象引用。
这是我的代码
public class MainActivity extends ActionBarActivity {
Button b1,b2;
ImageView iv;private File output=null;int CONTENT_REQUEST =9999;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button);
iv=(ImageView)findViewById(R.id.imageView);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
output = new File(dir, "CameraContentDemo.jpeg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output));
startActivityForResult(intent, CONTENT_REQUEST);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
// Bitmap bmp = BitmapFactory.decodeFile(urImageFilePath);
if (requestCode == CONTENT_REQUEST)
{
if (resultCode == RESULT_OK)
{
try{
Uri selectedImageUri = data.getData();
Log.e("selectedImageUri "," = " + selectedImageUri);
if(selectedImageUri!=null){
Bitmap bmp= BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImageUri));
Log.e("bmp "," = " + bmp);
iv.setImageBitmap(bmp);
Log.e("bmp ", " Displaying Imageview WIth Bitmap !!!! = ");
}
}
catch (FileNotFoundException fe)
{
fe.printStackTrace();
}
}}
这是Menifest.xml文件中的相机权限
<uses-feature android:name="android.hardware.camera"/>
<uses-permission android:name="android.permission.CAMERA"/>
这是我的Logcat错误
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=9999, result=-1, data=null} to activity {com.example.tazeen.demo3/com.example.tazeen.demo3.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.net.Uri android.content.Intent.getData()' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:3568)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3611)
at android.app.ActivityThread.access$1400(ActivityThread.java:147)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1345)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.net.Uri android.content.Intent.getData()' on a null object reference
at com.example.tazeen.demo3.MainActivity.onActivityResult(MainActivity.java:55)
at android.app.Activity.dispatchActivityResult(Activity.java:6135)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3564)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3611)
at android.app.ActivityThread.access$1400(ActivityThread.java:147)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1345)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
11-23 15:10:34.367 647-1092/? E/ActivityManager﹕ Invalid thumbnail dimensions: 384x384
答案 0 :(得分:1)
这是因为data.getData()
中不会出现从相机意图中获取的位图,如果您选择了已存在于磁盘中的文件,那么data.getData()
将是文件资源的URI。从Camera活动中获取的图片不会被存储,而是在内存中保存为Bitmap,并在intent的附加内容中传递给父活动的onActivityResult,你得到这个位图的方式是这样的
data.getExtras().get("data");
所以代码的变化就是这个,
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
// Bitmap bmp = BitmapFactory.decodeFile(urImageFilePath);
if (requestCode == CONTENT_REQUEST)
{
if (resultCode == RESULT_OK)
{
try{
Uri selectedImageUri = data.getData();
Log.e("selectedImageUri "," = " + selectedImageUri);
if(selectedImageUri!=null){
Bitmap bmp= BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImageUri));
Log.e("bmp "," = " + bmp);
iv.setImageBitmap(bmp);
Log.e("bmp ", " Displaying Imageview WIth Bitmap !!!! = ");
} else {
// If selectedImageUri is null check extras for bitmap
Bitmap bmp = (Bitmap) data.getExtras().get("data");
iv.setImageBitmap(bmp);
}
}
catch (FileNotFoundException fe)
{
fe.printStackTrace();
}
}}
编辑1
这就是你为结果启动相机活动的意图
final int CHOOSE_CAMERA = 232;
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CHOOSE_CAMERA);