从Android摄像头意图问题图像(image from camera intent issue i

2019-06-24 23:52发布

我整合了Android的Facebook,我想采取光度计时,将它保存到SD卡,然后将其上传到Facebook。

这里是我的代码:

photo_up=(Button)findViewById(R.id.camera_foto_button);
            photo_up.setOnClickListener(new View.OnClickListener() {
                   public void onClick(View v) {
                       final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
                       intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile(PlaceScreen.this)) );   
                       startActivityForResult(intent,CAMERA_REQUEST); 
                   }
                });

 private File getTempFile(Context context){  
          //it will return /sdcard/image.tmp  
          final File path = new File( Environment.getExternalStorageDirectory(), context.getPackageName() );  
          if(!path.exists()){  
            path.mkdir();  
          }  
          return new File(path, "image.png");  
        } 

和OnActivity结果

protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    switch(requestCode){
                case CAMERA_REQUEST:{
                    final File file = getTempFile(this);  
                    try {  
                      bmp = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.fromFile(file) );  
                      // do whatever you want with the bitmap (Resize, Rename, Add To Gallery, etc)  
                    } catch (FileNotFoundException e) {  
                      e.printStackTrace();  
                    } catch (IOException e) {  
                      e.printStackTrace();  
                    }  


                    ByteArrayOutputStream stream = new ByteArrayOutputStream();        
                    bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);         
                    byteArray = stream.toByteArray(); // convert camera photo to byte array  
                    Bundle params = new Bundle();       
                    params.putByteArray("picture", byteArray);      
                    params.putString("message", "Have fun");       
                    Utility.mAsyncRunner.request("me/photos", params,
                            "POST", new PhotoUploadListener(), null);
                    break;
                }

所以会发生什么是这样的:摄像头打开,imagae捕获并保存,但我得到一个强制关闭,并没有上载FB。

我检查我的手机上了。 logcat的是在这里:

05-31 02:50:19.437: E/AndroidRuntime(2470): FATAL EXCEPTION: main
05-31 02:50:19.437: E/AndroidRuntime(2470): java.lang.RuntimeException: Unable to resume activity {com.myname.package/com.myname.package.PlaceScreen}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1888, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.myname.package/com.myname.package.PlaceScreen}: java.lang.NullPointerException
05-31 02:50:19.437: E/AndroidRuntime(2470):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2124)
05-31 02:50:19.437: E/AndroidRuntime(2470):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2139)
05-31 02:50:19.437: E/AndroidRuntime(2470):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1672)
05-31 02:50:19.437: E/AndroidRuntime(2470):     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:2836)
05-31 02:50:19.437: E/AndroidRuntime(2470):     at android.app.ActivityThread.access$1600(ActivityThread.java:117)
05-31 02:50:19.437: E/AndroidRuntime(2470):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
05-31 02:50:19.437: E/AndroidRuntime(2470):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-31 02:50:19.437: E/AndroidRuntime(2470):     at android.os.Looper.loop(Looper.java:130)
05-31 02:50:19.437: E/AndroidRuntime(2470):     at android.app.ActivityThread.main(ActivityThread.java:3687)
05-31 02:50:19.437: E/AndroidRuntime(2470):     at java.lang.reflect.Method.invokeNative(Native Method)
05-31 02:50:19.437: E/AndroidRuntime(2470):     at java.lang.reflect.Method.invoke(Method.java:507)
05-31 02:50:19.437: E/AndroidRuntime(2470):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
05-31 02:50:19.437: E/AndroidRuntime(2470):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
05-31 02:50:19.437: E/AndroidRuntime(2470):     at dalvik.system.NativeStart.main(Native Method)
05-31 02:50:19.437: E/AndroidRuntime(2470): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1888, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.myname.package/com.myname.package.PlaceScreen}: java.lang.NullPointerException
05-31 02:50:19.437: E/AndroidRuntime(2470):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
05-31 02:50:19.437: E/AndroidRuntime(2470):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2111)
05-31 02:50:19.437: E/AndroidRuntime(2470):     ... 13 more
05-31 02:50:19.437: E/AndroidRuntime(2470): Caused by: java.lang.NullPointerException
05-31 02:50:19.437: E/AndroidRuntime(2470):     at com.myname.package.PlaceScreen.onActivityResult(PlaceScreen.java:325)
05-31 02:50:19.437: E/AndroidRuntime(2470):     at android.app.Activity.dispatchActivityResult(Activity.java:3908)
05-31 02:50:19.437: E/AndroidRuntime(2470):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
05-31 02:50:19.437: E/AndroidRuntime(2470):     ... 14 more

编辑:

好吧我BMP哪里是空。 这是为什么? IUS什么错?

bmp = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.fromFile(file) );  
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); 

任何人都可以找出为什么我的BMP为空,而我可以在画廊看到的图像。 这意味着,像正常拍摄,但没有得到为bmp = MediaStore.Images.Media的结果

Answer 1:

虽然使用通用代码可在.NET中调用相机和拍照和retrive它,我面临着三星设备的一些问题,当时同样的错误发生Failure delivering result ResultInfo ,但没有找到任何解决办法可能是因为任何API的其等级或设备扶养。

但要克服它,我写我的代码另一种方式做任务,该代码是在这里:

1)通话摄像头

        String _path = Environment.getExternalStorageDirectory()
        + File.separator + "TakenFromCamera.png";
        File file = new File(_path);
        Uri outputFileUri = Uri.fromFile(file);
        Intent intent = new Intent(
        android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        startActivityForResult(intent, 1212);

2)在ActivityResult得到位图

            if (requestCode == 1212) {
                String _path = Environment.getExternalStorageDirectory()
                + File.separator + "TakenFromCamera.png";
                mBitmap = BitmapFactory.decodeFile(_path);
                if (mBitmap == null) {
                    // bitmap still null
                } else {
                    // set bitmap in imageview
                }
            }


文章来源: image from camera intent issue in android