-2

I am developing an application that captures photo and saves it in internal device folder named "photo".I need to load the photo from "photo" folder to imageview.There is only one photo present in the folder.How to do this? Please help me.Thank you in advance. Below is the code that I have tried which loads photo from folder only if name of photo is displayed.

String path = Environment.getExternalStorageDirectory().getAbsolutePath()+ "/GeoPark/final_photo/20180504_002754.jpg";
        File imgFile = new File(path);
        if (imgFile.exists()) {
            imageView.setImageBitmap(decodeFile(imgFile));
        }
        else
            Toast.makeText(ViewActivity.this,"No Image File Present ",Toast.LENGTH_SHORT).show();
Manu
  • 11
  • 5

2 Answers2

1

Try this method .. in below method give proper file path.

private void loadImageFromStorage(String path)
{

try {
    File f=new File(path, "profile.jpg");
    Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));
        ImageView img=(ImageView)findViewById(R.id.imgPicker);
    img.setImageBitmap(b);
} 
catch (FileNotFoundException e) 
{
    e.printStackTrace();
}

}

Second things ..

File file = ....
Uri uri = Uri.fromFile(file);
imageView.setImageURI(uri);

alos i hope you add below permission into android manifest file ..

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Mobile Team ADR-Flutter
  • 12,062
  • 2
  • 29
  • 49
0

You could simply do a search about this, guaranteed results.

Anyway, Glide will help you with that

EDIT :

Taking picture using camera:

private void dispatchTakePictureIntent(Context context) {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(context.getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile(context);
        } catch (IOException ex) {
            // Error occurred while creating the File
            Snackbar.make(btn_open_camera, "Couldn't create a file for your picture!", Snackbar.LENGTH_LONG);
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(context,
                    "com.example.android.fileprovider",
                    photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, MY_PERMISSIONS_REQUEST_CAMERA);
        }
    }
}

private File createImageFile(Context context) throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = context.getFilesDir();
    Log.i(TAG, "StorageDir : " + storageDir);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = image.getAbsolutePath();
    Log.i(TAG, "mCurrentPhotoPath : " + mCurrentPhotoPath);

    return image;
}

Now you have the image path stored in mCurrentPhotoPath which is a String declared in current activity. As you said, you will need this in next activity, to do that you can take a look at this answer

Ionut J. Bejan
  • 655
  • 11
  • 27