在 Android 7.0 中从图片库路径创建位图?



我有一个问题,我正在尝试将图像库路径转换为位图 这是我的代码。请帮助我。

错误

Unable to decode stream: java.io.FileNotFoundException: 
/storage/emulated/0/DCIM/100ANDRO/DSC_0013.JPG
(No such file or directory)

法典

public static final int MY_BACKGROUND_JOB = 0;
JobParameters mRunningParams;
public Context context;
static final Uri MEDIA_URI = Uri.parse("content://" + MediaStore.AUTHORITY + "/");
static final List<String> EXTERNAL_PATH_SEGMENTS = MediaStore.Images.Media.EXTERNAL_CONTENT_URI.getPathSegments();
static final String[] PROJECTION = new String[]{
MediaStore.Images.ImageColumns._ID, MediaStore.Images.ImageColumns.DATA};
static final int PROJECTION_ID = 0;
static final int PROJECTION_DATA = 1;
static final String DCIM_DIR = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath();

@Override
public boolean onStartJob(JobParameters params) {
Log.i("PhotosContentJob", "JOB STARTED!");
_sharedPreferencesUtility = new SharedPreferencesUtility(getApplicationContext());
_globalTypeface = new Global_Typeface(getApplication());
_mainContext = new MainActivity();
SetGPSLocation();
SetPreferenceData();
SetCheckLocationFormat();
mRunningParams = params;
mJobHandler.sendMessage(Message.obtain(mJobHandler, 1, params));
StringBuilder sb = new StringBuilder();
if (params.getTriggeredContentAuthorities() != null) {
boolean rescanNeeded = false;
if (params.getTriggeredContentUris() != null) {
ArrayList<String> ids = new ArrayList<>();
for (Uri uri : params.getTriggeredContentUris()) {
List<String> path = uri.getPathSegments();
if (path != null && path.size() == EXTERNAL_PATH_SEGMENTS.size() + 1) {
// This is a specific file.
ids.add(path.get(path.size() - 1));
} else {
rescanNeeded = true;
}
}
if (ids.size() > 0) {
StringBuilder selection = new StringBuilder();
for (int i = 0; i < ids.size(); i++) {
if (selection.length() > 0) {
selection.append(" OR ");
}
selection.append(MediaStore.Images.ImageColumns._ID);
selection.append("='");
selection.append(ids.get(i));
selection.append("'");
}
Cursor cursor = null;
boolean haveFiles = false;
try {
cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, PROJECTION, selection.toString(), null, null);
while (cursor.moveToNext()) {
String dir = cursor.getString(PROJECTION_DATA);
if (dir.startsWith(DCIM_DIR)) {
if (!haveFiles) {
haveFiles = true;
}
sb.append(dir);
sb.append("n");
}
}
} catch (SecurityException e) {
sb.append("Error: no access to media!");
} finally {
if (cursor != null) {
cursor.close();
}
}
}
} else {
rescanNeeded = true;
}
if (rescanNeeded) {
sb.append("Photos rescan needed!");
}
} else {
sb.append("(No photos content)");
}
Log.e("TAG_URL_24", sb.toString());
_st_ImagePath = sb.toString();
Bitmap bitmap = BitmapFactory.decodeFile(stImagePath);
Bitmap.Config config = bitmap.getConfig();
if (config == null) {
config = Bitmap.Config.ARGB_8888;
}
mHandler.postDelayed(mWorker, 1);
return true;
}

谢谢你。

这是适合您的解决方案

有了这个参考,你的查询有一个解决方案

在程序中使用DIR的字符串值作为文件路径,您可以获得结果。

File sd = Environment.getExternalStorageDirectory();
File image = new File("YOUR FILE PATH", imageName);
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(),bmOptions);
bitmap = Bitmap.createScaledBitmap(bitmap,parent.getWidth(),parent.getHeight(),true);
imageView.setImageBitmap(bitmap);

最新更新