检索照片时路径问题

时间:2018-02-01 11:12:51

标签: android android-external-storage android-fileprovider

拍摄照片并将其设置为ImageView时,路径出现问题。 当我使用这种方法时

File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);

使用的路径是

02-01 12:07:24.068 31618-31618/disco.unimib.it.polapp D/ciao: /storage/emulated/0/Android/data/disco.unimib.it.polapp/files/Pictures

但是当我像这样分配uri时

photoUri = FileProvider.getUriForFile(NotifyActivity.this, "com.example.android.fileprovider", photoFile);

uri显示

02-01 12:07:24.071 31618-31618/disco.unimib.it.polapp D/ciao: content://com.example.android.fileprovider/images/Android/data/disco.unimib.it.polapp/files/Pictures/JPEG_20180201_120724_1953425230.jpg

当我尝试将照片保存到变量时,错误是

02-01 12:07:29.975 31618-31618/disco.unimib.it.polapp E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /images/Android/data/disco.unimib.it.polapp/files/Pictures/JPEG_20180201_120724_1953425230.jpg (No such file or directory)

这是完整的代码

NotifyActivity.java:

public class NotifyActivity extends AppCompatActivity {

Bitmap image;

String mCurrentPhotoPath;

File photoFile=null;

Uri photoUri;

Intent TakePictureIntent;

ImageView photoSaved;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_notify);


    final Toolbar toolbar= (Toolbar) findViewById(R.id.toolbar4);
    setSupportActionBar(toolbar);
    setTitle(R.string.problem);

    final Button button3=(Button) findViewById(R.id.button3);
    final Button button4=(Button) findViewById(R.id.button4);
    photoSaved=(ImageView) findViewById(R.id.photoSaved);

    button4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            TakePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            if (TakePictureIntent.resolveActivity(getPackageManager()) != null) {
                if (ContextCompat.checkSelfPermission(NotifyActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(NotifyActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
                } else {
                    try {
                        photoFile = createImageFile();
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                    if (photoFile != null) {
                        photoUri = FileProvider.getUriForFile(NotifyActivity.this, "com.example.android.fileprovider", photoFile);
                        Log.d("ciao", String.valueOf(photoUri));
                        TakePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
                        startActivityForResult(TakePictureIntent, 50);
                    }

                }


            }
        }
    });

    button3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Snackbar.make(findViewById(R.id.notify),R.string.notificationsent,Snackbar.LENGTH_LONG);
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);
    image= BitmapFactory.decodeFile(photoUri.getPath());
    if(image!=null){
        Snackbar.make(findViewById(R.id.notify),R.string.photosaved,Snackbar.LENGTH_LONG).show();
        photoSaved.setImageBitmap(image);
    }
}

private File createImageFile() throws IOException {
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    Log.d("ciao", String.valueOf(getExternalFilesDir(Environment.DIRECTORY_PICTURES)));
    File image = File.createTempFile(imageFileName, ".jpg", storageDir);
    mCurrentPhotoPath = image.getAbsolutePath();
    return image;
}

@Override
public void onRequestPermissionsResult(int requestCode, String [] permissions,int [] grantResults){
    if (grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
        try{
            photoFile = createImageFile();
        } catch(IOException ex){
            ex.printStackTrace();
        }
        if (photoFile != null) {
            photoUri = FileProvider.getUriForFile(NotifyActivity.this, "com.example.android.fileprovider", photoFile);
            TakePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
            startActivityForResult(TakePictureIntent, 50);
        }
    }else{
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

}

file_paths.xml:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
   <external-path name="images" path="." />
</paths>

我已经将提供者放在我的androidmanifest.xml

2 个答案:

答案 0 :(得分:1)

java.io.FileNotFoundException被抛出是因为您正在使用FileProvider的Uri,这会创建

  

content:// Uri for a file。

虽然BitmapFactory.decodeFile(String path)需要

  

要解码的文件的完整路径名。

所以,你需要使用:

BitmapFactory.decodeFile(photoFile.getAbsolutePath());

答案 1 :(得分:0)

<external-path。更改为<external-files-path

image= BitmapFactory.decodeFile(photoUri.getPath());

替换为

InputStream is = getContentResolver().openInputStream(photoUri);
image= BitmapFactory.decodeStream(is);