【问题标题】:Android camera photo take FileNotFoundExceptionAndroid 相机拍照 FileNotFoundException
【发布时间】:2014-05-26 18:26:35
【问题描述】:

原始:在我的应用程序中,我从代码中调用本机相机,我打算从相机拍照并获取其文件路径也将其显示在图像视图中。

下面的代码是从 AlertDialog.Builder 调用的

//run camera
            builder.setNegativeButton("Resim Cek", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //native camera
                     // here,counter will be incremented each time,and the picture taken by camera will be stored as 1.jpg,2.jpg and likewise.
                    count++;
                    String file = dir+count+".jpg";
                    File newfile = new File(file);
                    try {
                        newfile.createNewFile();
                    } catch (IOException e) {}       

                    Uri outputFileUri = Uri.fromFile(newfile);

                    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
                    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

                    startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);  
                    dialog.dismiss();


                }
            });

在onActivityResult中

if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
        Log.d("CameraDemo", "Pic saved");
        final File file = getTempFile(this);
        try
        {
        Media.getBitmap(getContentResolver(), Uri.fromFile(file) );
        String image_string = Uri.fromFile(file).toString();
        Bitmap bm = Bitmap.createScaledBitmap(
            BitmapFactory.decodeFile(image_string),
            getResources().getDisplayMetrics().widthPixels,
            getResources().getDisplayMetrics().heightPixels, 
            true);
        bmpPhoto = bm;
        bmpPhotoPath =image_string;
        imageView.setImageBitmap(bmpPhoto);
        removePhoto.setVisibility(View.VISIBLE);
            }
        catch (FileNotFoundException e)
            {
            Toast.makeText(getApplicationContext(), "file not found exception", Toast.LENGTH_SHORT).show();
            }
        catch (IOException e)
            {
            Toast.makeText(getApplicationContext(), "ioexception", Toast.LENGTH_SHORT).show();
            }
     } 

而且它总是出现 FileNotFoundException,我该如何解决这个问题?

由于我使用的是 try catch ,它不会给出太多日志

 05-26 10:04:12.620: D/CameraDemo(3794): Pic saved

改变onActivityResult后

 if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
        Log.d("CameraDemo", "Pic saved");
        //final File file = getTempFile(this);
        File outputDir = ContactFormActivity.this.getCacheDir(); // context being the Activity pointer
        File file = null;
        try {
            file = File.createTempFile("prefix", "extension", outputDir);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        try
        {
        Media.getBitmap(getContentResolver(), Uri.fromFile(file) );
        String image_string = Uri.fromFile(file).toString();
        Bitmap bm = Bitmap.createScaledBitmap(
            BitmapFactory.decodeFile(image_string),
            getResources().getDisplayMetrics().widthPixels,
            getResources().getDisplayMetrics().heightPixels, 
            true);
        bmpPhoto = bm;
        bmpPhotoPath =image_string;
        imageView.setImageBitmap(bmpPhoto);
        removePhoto.setVisibility(View.VISIBLE);
            }
        catch (FileNotFoundException e)
            {
            Toast.makeText(getApplicationContext(), "file not found exception", Toast.LENGTH_SHORT).show();
            }
        catch (IOException e)
            {
            Toast.makeText(getApplicationContext(), "ioexception", Toast.LENGTH_SHORT).show();
            }
     }

我的日志在这里

05-26 10:29:59.170: D/CameraDemo(6559): Pic saved
05-26 10:29:59.190: D/skia(6559): --- SkImageDecoder::Factory returned null
05-26 10:29:59.190: I/System.out(6559): Not a DRM File, opening notmally
05-26 10:29:59.190: D/AndroidRuntime(6559): Shutting down VM
05-26 10:29:59.190: W/dalvikvm(6559): threadid=1: thread exiting with uncaught exception (group=0x40c551f8)
05-26 10:29:59.230: E/AndroidRuntime(6559): FATAL EXCEPTION: main
05-26 10:29:59.230: E/AndroidRuntime(6559): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=null} to activity {com.company.project/com.company.project.ContactFormActivity}: java.lang.NullPointerException
05-26 10:29:59.230: E/AndroidRuntime(6559):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2992)
05-26 10:29:59.230: E/AndroidRuntime(6559):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3035)
05-26 10:29:59.230: E/AndroidRuntime(6559):     at android.app.ActivityThread.access$1100(ActivityThread.java:127)
05-26 10:29:59.230: E/AndroidRuntime(6559):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1189)
05-26 10:29:59.230: E/AndroidRuntime(6559):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-26 10:29:59.230: E/AndroidRuntime(6559):     at android.os.Looper.loop(Looper.java:137)
05-26 10:29:59.230: E/AndroidRuntime(6559):     at android.app.ActivityThread.main(ActivityThread.java:4507)
05-26 10:29:59.230: E/AndroidRuntime(6559):     at java.lang.reflect.Method.invokeNative(Native Method)
05-26 10:29:59.230: E/AndroidRuntime(6559):     at java.lang.reflect.Method.invoke(Method.java:511)
05-26 10:29:59.230: E/AndroidRuntime(6559):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:978)
05-26 10:29:59.230: E/AndroidRuntime(6559):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
05-26 10:29:59.230: E/AndroidRuntime(6559):     at dalvik.system.NativeStart.main(Native Method)
05-26 10:29:59.230: E/AndroidRuntime(6559): Caused by: java.lang.NullPointerException
05-26 10:29:59.230: E/AndroidRuntime(6559):     at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:432)
05-26 10:29:59.230: E/AndroidRuntime(6559):     at com.company.project.ContactFormActivity.onActivityResult(ContactFormActivity.java:419)
05-26 10:29:59.230: E/AndroidRuntime(6559):     at android.app.Activity.dispatchActivityResult(Activity.java:4653)
05-26 10:29:59.230: E/AndroidRuntime(6559):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2988)
05-26 10:29:59.230: E/AndroidRuntime(6559):     ... 11 more

【问题讨论】:

  • 我可以让你登录消息吗
  • image_string - 字符串值是什么
  • 你是否使用了所有权限

标签: android android-camera filenotfoundexception


【解决方案1】:

我已经改变了对这个问题的看法,越简单越好

在一些全球范围内

int TAKE_PHOTO_CODE = 1888;

在 buttononclick 或您调用相机意图的地方

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);  

在onActivityResult中

if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
 if(data != null)
        {           
            Uri selectedImageUri = data.getData();
            String filestring = selectedImageUri.getPath();

            Bitmap bm = (Bitmap) data.getExtras().get("data");
            bmpPhoto = bm;
            bmpPhotoPath =filestring;
            imageView.setImageBitmap(bmpPhoto);
            removePhoto.setVisibility(View.VISIBLE);

        }}

【讨论】:

    【解决方案2】:
    File outputDir = context.getCacheDir(); // context being the Activity pointer
    File outputFile = File.createTempFile("prefix", "extension", outputDir);
    

    试试这个方法创建你的临时文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多