1

I have an issue where I'm trying to convert Image gallery path into Bitmap Here is my code. Please help me.

Error

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

Code

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

Thanking you.

k3b
  • 14,517
  • 7
  • 53
  • 85
Sumit Pansuriya
  • 543
  • 10
  • 23
  • did you add run time permission – AskNilesh Jul 19 '17 at 13:06
  • and run time permission – AskNilesh Jul 19 '17 at 13:07
  • yes runtime also ===: private void requestPermission() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_STORAGE_PERMISSION); } else { StartActivity(); } } – Sumit Pansuriya Jul 19 '17 at 13:08

1 Answers1

0

Here is the solution for you

With this reference there is a solution for your query

use the string value of dir in your program as file path you can get the result.

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);
Arunkumar K
  • 171
  • 1
  • 4
  • 13