我在imgFile.exist()
上的if条件是假的,但是当Log.d打印出结果时,我可以看到该文件存在于路径中。
我只是尝试从路径加载图像,将其作为活动微缩图像的背景
我的清单android.permission.WRITE_EXTERNAL_STORAGE
加载图片:
File imgFile = new File(getRowItem.getImgURI().trim());
Log.d("TAGG", "FILE PATH: " + imgFile.getPath());
// imgFile's path: /storage/emulated/0/TravelDiary/img_1461684314337.jpg
if(imgFile.exists()){
//it never reach inside here
Log.d("TAGG", "FILE ABSOLUTE PATH: " + imgFile.getAbsolutePath());
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
Drawable drawable = new BitmapDrawable(getResources(), myBitmap);
imageView.setBackground(drawable);
}
保存图片:
当来自intentChooser的用户选择从相机捕捉图像或从gallary中选择图像时,会保存图像
public void ImagePicker(View v){
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
if (PackageManager.PERMISSION_GRANTED == ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) && PackageManager.PERMISSION_GRANTED == ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)) {
final File rootdir = new File(Environment.getExternalStorageDirectory() + File.separator + "TravelDiary");
rootdir.mkdirs();
final String filename = "img_" + System.currentTimeMillis() + ".jpg";
final File sdImageMainDirecotry = new File(rootdir, filename);
outputFileUri = Uri.fromFile(sdImageMainDirecotry);
...
...
} else {
//if permission is not granted, then we ask for it
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
}else{
Toast.makeText(this, "External storage not available", Toast.LENGTH_SHORT).show();
}
}
我的清单中的权限:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.muddii.traveldiary">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
...
...
...
</manifest>
答案 0 :(得分:0)
在清单中添加此内容。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
答案 1 :(得分:0)
gradle中的SDK值是多少?如果是23,那么你需要在运行时请求许可。
http://developer.android.com/training/permissions/requesting.html
答案 2 :(得分:0)
试试这个而不是imgFile.exists()
File file = new File(path);
if (!file.isFile()) {
//your code
}else{
//your code
}