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

时间:2017-07-19 13:05:05

标签: java android bitmap

我遇到了一个问题,我试图将图片库路径转换为Bitmap 这是我的代码。请帮帮我。

错误

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

感谢你。

1 个答案:

答案 0 :(得分:0)

这是为您提供的解决方案

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