2017-01-04 36 views
0

我试图将相机拍摄的图像保存到设备的INTERNAL存储器中,然后将其作为ImageView使用,但无法访问该文件。这全是为了教育目的。我有一个按钮可以将相机应用程序作为子活动。Android:无法从Android设备上的内部存储器访问图像文件

public void addListener() { 
    Button b = (Button)findViewById(R.id.snap); 
    b.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      File picturesDirectory; 
      picturesDirectory = getFilesDir(); 
      imageFile = new File(picturesDirectory, "passspoints_image"); 

      Log.d(DEBUGTAG, imageFile.getAbsolutePath()); 

      Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile)); 
      startActivityForResult(i, PHOTO_TAKEN); 
     } 
    }); 
} 

然后我试图访问该文件。

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    switch(requestCode) { 
     case PHOTO_TAKEN: 
      Log.d(DEBUGTAG, imageFile.getAbsolutePath()); 
      Bitmap photo = BitmapFactory.decodeFile(imageFile.getAbsolutePath()); 
      try { 
       photo = rotateImageIfRequired(photo, Uri.fromFile(imageFile)); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      if(photo != null) { 
       ImageView imageView = (ImageView)findViewById(R.id.imageView); 
       imageView.setImageBitmap(photo); 
      } else { 
       Toast.makeText(this, "Unable to read photo file", Toast.LENGTH_LONG). 
         show(); 
      } 
      break; 
    } 
} 

这是从调试标签的路径:

/data/data/tk.crackedbunny.www.takingphotostest/files/passspoints_image 

这是错误消息:

E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /data/data/tk.crackedbunny.www.takingphotostest/files/passspoints_image: open failed: ENOENT (No such file or directory) 

在此之前,我已成功这个使用外部存储做,但如果外部存储器不可用,我想使用内部存储器。

谢谢。

回答

0

passspoints_image图片名称?如何为它添加扩展名,例如passspoints_image.jpg

或者您有AndroidManifest.xml文件的读取/写入权限吗?

+0

是的,** passpoins_image **是文件的名称。正如我刚才提到的那样,当我使用外部存储时,它工作的很好,当我在Android清单中有渗透时,但我认为它们只是用于读/写才能使用外部存储外部,而不是内部存储。无论如何,我有这个: '<使用权限 android:name =“android.permission.WRITE_EXTERNAL_STORAGE” />' – dkk6

+0

不幸的是,使用.jpg和.bmp,它仍然无法正常工作。 – dkk6

相关问题