简体   繁体   English

来自相机意图问题的图像在Android中

[英]image from camera intent issue in android

I am integrating facebook with android and I want when taking a phot , save it to sdcard and then upload it to facebook. 我正在将Facebook与android集成在一起,我想在拍照时将其保存到sdcard,然后将其上传到Facebook。

Here is my code: 这是我的代码:

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");  
        } 

and the OnActivity Result 和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;
                }

So what happens is this: Camera opens, imagae captured and saved but I get a force close and it is not uploaded on fb. 所以这是怎么回事:相机打开,捕获并保存了图像,但是我被迫关闭,并且没有上传到fb。

I checked it on my phone,too. 我也是在手机上检查过的。 Logcat is here: 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

edit: 编辑:

Ok my bmp where is null. 好的,我的bmp在哪里为null。 Why is that? 这是为什么? What ius wrong? 怎么了?

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

can anyone find out why my bmp is null while I can see the image in the gallery. 在我可以看到图库中的图像时,谁能找出为什么我的bmp为空的原因。 That means that image is taken normally but nothing gets as a result of bmp=MediaStore.Images.Media 这意味着可以正常拍摄图像,但是由于bmp = MediaStore.Images.Media而没有任何结果

While using the generic code available on net to call camera and take photo and retrive it, I faced some problem in SAMSUNG device at that time the same error occured Failure delivering result ResultInfo and couldn't find any solution may be its because of any API Level or Device dependancy. 当使用网上可用的通用代码调用相机并拍照并取回它时,我在SAMSUNG设备中遇到了同样的错误,当时发生了相同的错误Failure delivering result ResultInfo并且找不到任何解决方案,可能是因为有任何API级别或设备依赖性。

But to overcome it I wrote my code another way to do the task, The code is here: 但是为了克服它,我编写了另一种执行任务的代码,代码在这里:

1) To Call Camera 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) In ActivityResult to get Bitmap 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
                }
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM