我遇到了一个奇怪的问题,在调用MediaStore.Images.Media.insertImage时,它有时会返回null uri,但我发现它可以运行一两次,我找不到它的规则。 请给予帮助,非常感谢!
logcat是:
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)
我的代码是:
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);
}
}
答案 0 :(得分:1)
我有类似的问题。如果您的设备使用Marshmallow,并且目标API为23,则需要在运行时请求权限(Manifest.permission.WRITE_EXTERNAL_STORAGE) 。
另请参阅:https://medium.com/ribot-labs/exploring-the-new-android-permissions-model-ba1d5d6c0610#.y84j1nene
答案 1 :(得分:0)
onclick检查Android版本和权限,如下所示:
@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);
}
和checkIfAlreadyhavePermission()
方法正文如下:
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;
}