-1

I am creating an android app where I take a picture using camera intent, then store the picture in the picture directory. Then my app required to upload it. But, As I take the first picture from the app I can't see it in the gallery. But, from the second picture I take I can see them in the gallery. What to do?

here is my code: Here I display my pic on an image button:

 mImageSelect.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                Intent galleryIntent=new Intent(Intent.ACTION_GET_CONTENT);
                galleryIntent.setType("image/*");
                startActivityForResult(galleryIntent,GALLERY_REQUEST);
            }catch (Exception e){
                Toast.makeText(getApplicationContext(),"There was an ERROR: "+e,Toast.LENGTH_LONG).show();
                Intent intent=new Intent(PostActivity.this,AllPosts.class);
            }

        }
    });

// I am saving it with mediastore.

MediaScannerConnection.scanFile(HomeActivity.this,
                            new String[] {imageFile.getAbsolutePath()},
                            new String[] {"image/jpeg"},null);

            sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(imageFile)));
Soumya Rauth
  • 1,163
  • 5
  • 16
  • 32
  • There is no code that takes a picture or saves a picture. Nor can we see when and how you invoke the mediascanner. Moreover you did not tell why you would save a picture yourself where the camera app would gladly do it for you. – greenapps Dec 13 '16 at 12:18
  • `Here I display my pic on an image button:`. No. Not at all. There you start a file picker for get content. That's all. But what has displaying on an image on a button to do with taking a picture and uploading that picture? – greenapps Dec 13 '16 at 12:24

2 Answers2

0

Use below method for API level below 19 to scan media file.

context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
            + Environment.getExternalStorageDirectory()))); 

For API level 19 or above scan media file like below.

private void scanFile(String path) {

    MediaScannerConnection.scanFile(MainActivity.this,
         new String[] { path }, null,
         new MediaScannerConnection.OnScanCompletedListener() {

             public void onScanCompleted(String path, Uri uri) {
                    Log.i("TAG", "Finished scanning " + path);
             }
    });
}
Priyank Patel
  • 12,244
  • 8
  • 65
  • 85
0
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
{
    Intent mediaScanIntent = new     Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        File f = new File("file://"+     Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES));
    Uri contentUri = Uri.fromFile(f);
    mediaScanIntent.setData(contentUri);
    this.sendBroadcast(mediaScanIntent);
}
else
{
    sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,     Uri.parse("file://" + Environment.getExternalStorageDirectory())));
}
Nirmal Shethwala
  • 268
  • 1
  • 15