0

Is there any way to get the image id from the following ActivityResult method?

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        if(requestCode == IMAGE_CAPTURE){
            if(resultCode == RESULT_OK){
                try{
                    Bitmap b1 = MediaStore.Images.Media.getBitmap(getContentResolver(),imageUri);

                    System.out.println("GGEGEGGEGEGEGGEGEGEGEG" + imageUri.getPath());
//                   Intent i2 = new Intent(Intent.ACTION_VIEW, imageUri);
//                   startActivity(i2);
                }
                catch(FileNotFoundException e ){
                    System.out.println("OnActivityResult hat einen Fehler geworfen");
                } catch (IOException e) {
                    System.out.println("ONACTIVITYRESULT hat einen Fehler geworfen");
                }
            }
        }
    }

I need it to get the thumbnail to a certain image.

EDIT: I capture the image like this:

private void takePic() {
        ContentValues values = new ContentValues();
        String TITLE = null;
        values.put(MediaColumns.TITLE, TITLE);
        String DESCRIPTION  = null;
        long id = (Long) null;
        values.put(ImageColumns.DESCRIPTION, DESCRIPTION);
        values.put(MediaColumns.MIME_TYPE, "image/jpeg");
        values.put(MediaColumns._ID, id);
        imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);


        System.out.println("VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVvv" + imageUri.getPath());
        startActivityForResult(intent, IMAGE_CAPTURE);


    }
user896692
  • 2,351
  • 7
  • 36
  • 57

1 Answers1

0

Once you have created the imageUri, you can get the ID from that like this:

    String[] columns = {MediaColumns._ID}; 
    Cursor cursor =  getContentResolver().query(imageUri, columns, null, null, null); 
    cursor.moveToFirst(); 
    long id = cursor.getLong(0);

NOTE: You should probably remove this line from the code in takePic() because the row ID is created for your by the content provider:

    values.put(MediaColumns._ID, id);
David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.database.Cursor.moveToFirst() – Ram Oct 24 '17 at 12:43
  • @Ram you should open a new question with your problem. Post your code and your logcat with stacktrace. A comment on my answer isn't the proper place to get help. – David Wasser Oct 24 '17 at 14:00