-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
  • 6

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"/>
  • I want to load all photos from folder without specifying the name – Manu May 07 '18 at 06:42
  • if you are using single imageview it show only one image at a time. –  May 07 '18 at 06:45
  • The folder will have different image names as image name is a timestamp.So how is it possible to specigy the name – Manu May 07 '18 at 06:45
  • The image will change from time to time and how will I know the name of image in advance – Manu May 07 '18 at 06:47
  • image name is required without image name how to know imageview which image to load. –  May 07 '18 at 06:53
  • That is the problem how to specify the name of image.Actually the user clicks a photo and saves it in folder "photo".The image from "photo" folder must be loaded to imageview.But I cannot give a specific name in code – Manu May 07 '18 at 07:02
  • The captured images that are saved in folder must be displayed in imageview in another activity – Manu May 07 '18 at 07:23
  • that time get path of capture image and pass into file. and used below code.. ImageView.setImageURI(Uri uri) –  May 07 '18 at 07:36
  • i update my answer and provide second things to load image. –  May 07 '18 at 07:38
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
  • 734
  • 11
  • 28