gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 11:18:26 26 4
gpt4 key购买 nike

我在尝试将图片库路径转换为位图时遇到问题这是我的代码。请帮助我。

错误

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;
}

谢谢你。

最佳答案

这是适合您的解决方案

With this reference there is a solution for your query

使用程序中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);

关于java - 在 Android 7.0 中从图片库路径创建位图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45191519/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com