MediaStore.Images.Media.insertImage return null sometimes

5.3k views Asked by At

I encountered strange problem, when calling MediaStore.Images.Media.insertImage, it returns null uri sometimes, but I found it can work well one or two times, I can't find it's rule. Please give help, thanks a lot!

the logcat is:

06-18 11:09:24.559: E/MediaStore(13050): Thumbnail is not generated in query() with blockingUri. Return null! 
06-18 11:09:24.567: E/MediaStore(13050): Failed to insert image
06-18 11:09:24.567: E/MediaStore(13050): java.lang.NullPointerException
06-18 11:09:24.567: E/MediaStore(13050):    at android.provider.MediaStore$Images$Media.StoreThumbnail(MediaStore.java:755)
06-18 11:09:24.567: E/MediaStore(13050):    at android.provider.MediaStore$Images$Media.insertImage(MediaStore.java:824)
06-18 11:09:24.567: E/MediaStore(13050):    at net.blogjava.mobile.camera.Main.onActivityResult(Main.java:57)
06-18 11:09:24.567: E/MediaStore(13050):    at android.app.Activity.dispatchActivityResult(Activity.java:4649)
06-18 11:09:24.567: E/MediaStore(13050):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2990)
06-18 11:09:24.567: E/MediaStore(13050):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3037)
06-18 11:09:24.567: E/MediaStore(13050):    at android.app.ActivityThread.access$1100(ActivityThread.java:127)
06-18 11:09:24.567: E/MediaStore(13050):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1191)
06-18 11:09:24.567: E/MediaStore(13050):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-18 11:09:24.567: E/MediaStore(13050):    at android.os.Looper.loop(Looper.java:137)
06-18 11:09:24.567: E/MediaStore(13050):    at android.app.ActivityThread.main(ActivityThread.java:4512)
06-18 11:09:24.567: E/MediaStore(13050):    at java.lang.reflect.Method.invokeNative(Native Method)
06-18 11:09:24.567: E/MediaStore(13050):    at java.lang.reflect.Method.invoke(Method.java:511)
06-18 11:09:24.567: E/MediaStore(13050):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:982)
06-18 11:09:24.567: E/MediaStore(13050):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
06-18 11:09:24.567: E/MediaStore(13050):    at dalvik.system.NativeStart.main(Native Method)

My Code is:

import java.io.File;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

public class Main extends Activity implements OnClickListener
{
  public ImageView imageView;
  private ImageView ivFocus;

  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button btnTakePicture = (Button) findViewById(R.id.btnTakePicture);
    btnTakePicture.setOnClickListener(this);
    imageView = (ImageView) findViewById(R.id.imageview);
    ivFocus = new ImageView(this);
    Toast.makeText(this, "onCreate", Toast.LENGTH_LONG).show();
  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data)
  {
    if (requestCode == 1)
    {
      if (resultCode == Activity.RESULT_OK)
      {
        Bitmap cameraBitmap;
        try {
          if(data != null)
          {
            cameraBitmap = (Bitmap) data.getExtras().get("data");
          }
          else
          {
            cameraBitmap = BitmapFactory.decodeFile("/sdcard/1test.jpg");
          }
          imageView.setImageBitmap(cameraBitmap);

          ContentResolver cr = getContentResolver();
          String ret = MediaStore.Images.Media.insertImage(cr, cameraBitmap, "my_pic", "my picture");
          setTitle(ret);
        } catch (Exception e) {
          Toast.makeText(this, e.getClass()+":"+e.getMessage(), Toast.LENGTH_LONG).show();
        }
      }
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

  @Override
  public void onClick(View view)
  {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    //Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File("/sdcard/1test.jpg")));
    startActivityForResult(intent, 1);
  }
}
2

There are 2 answers

0
shawn_wx On

I had a similar issue. If your device is using Marshmallow, and your target API is 23, you need to ask for permission (Manifest.permission.WRITE_EXTERNAL_STORAGE) at run time.

See also: https://medium.com/ribot-labs/exploring-the-new-android-permissions-model-ba1d5d6c0610#.y84j1nene

0
Hadi Samadbin On

onclick check for android version and permission as below:

@Override
public void onClick(View view)
{

 int MyVersion = Build.VERSION.SDK_INT;
    if (MyVersion > Build.VERSION_CODES.LOLLIPOP_MR1) {
            checkIfAlreadyhavePermission();
    }
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File("/sdcard/1test.jpg")));
startActivityForResult(intent, 1);
}

and checkIfAlreadyhavePermission() method body will be as below:

private boolean checkIfAlreadyhavePermission() {


            checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
                 if((checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                            != PackageManager.PERMISSION_GRANTED))) {

                    //show dialog to ask permission
                    ActivityCompat.requestPermissions(this,
                            new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
                                    android.Manifest.permission.wRITE_EXTERNAL_STORAGE},
                            1);
                    return true;
            } else

                   return false;

    }