当我从SD卡中拍摄图像时,它在我的移动设备(micromax canvas hd)中工作得很好,但它不能在galaxy nexus中工作。在galaxy nexus中,它显示空指针异常。
if (requestCode == GALLERY_PICTURE) {
if (resultCode == RESULT_OK) {
if (data != null) {
// our BitmapDrawable for the thumbnail
BitmapDrawable bmpDrawable = null;
// try to retrieve the image using the data from the intent
Cursor cursor = getContentResolver().query(data.getData(),
null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
Log.d("file path","file path name"+selectedImagePath);
int idx = cursor.getColumnIndex(ImageColumns.DATA);
String fileSrc = cursor.getString(idx);
bitmap = BitmapFactory.decodeFile(fileSrc); // load
// preview
// image
bitmap = Bitmap.createScaledBitmap(bitmap,
100, 100, false);
// bmpDrawable = new BitmapDrawable(bitmapPreview);
image.setImageBitmap(bitmap);
data1 = null;
这是日志文件---
12-30 14:37:26.480: E/CursorWindow(14558): Failed to read row 0, column -1 from a CursorWindow which has 1 rows, 6 columns.
12-30 14:37:26.490: E/AndroidRuntime(14558): FATAL EXCEPTION: main
12-30 14:37:26.490: E/AndroidRuntime(14558): Process: com.app.postevery, PID: 14558
12-30 14:37:26.490: E/AndroidRuntime(14558): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/image:30713 flg=0x1 }} to activity {com.app.postevery/com.app.postevery.PostEvery}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
12-30 14:37:26.490: E/AndroidRuntime(14558): at android.app.ActivityThread.deliverResults(ActivityThread.java:3365)
12-30 14:37:26.490: E/AndroidRuntime(14558): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3408)
12-30 14:37:26.490: E/AndroidRuntime(14558): at android.app.ActivityThread.access$1300(ActivityThread.java:135)
12-30 14:37:26.490: E/AndroidRuntime(14558): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
12-30 14:37:26.490: E/AndroidRuntime(14558): at android.os.Handler.dispatchMessage(Handler.java:102)
12-30 14:37:26.490: E/AndroidRuntime(14558): at android.os.Looper.loop(Looper.java:136)
12-30 14:37:26.490: E/AndroidRuntime(14558): at android.app.ActivityThread.main(ActivityThread.java:5017)
12-30 14:37:26.490: E/AndroidRuntime(14558): at java.lang.reflect.Method.invokeNative(Native Method)
12-30 14:37:26.490: E/AndroidRuntime(14558): at java.lang.reflect.Method.invoke(Method.java:515)
12-30 14:37:26.490: E/AndroidRuntime(14558): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-30 14:37:26.490: E/AndroidRuntime(14558): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-30 14:37:26.490: E/AndroidRuntime(14558): at dalvik.system.NativeStart.main(Native Method)
12-30 14:37:26.490: E/AndroidRuntime(14558): Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
12-30 14:37:26.490: E/AndroidRuntime(14558): at android.database.CursorWindow.nativeGetString(Native Method)
12-30 14:37:26.490: E/AndroidRuntime(14558): at android.database.CursorWindow.getString(CursorWindow.java:434)
12-30 14:37:26.490: E/AndroidRuntime(14558): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
12-30 14:37:26.490: E/AndroidRuntime(14558): at android.database.CursorWrapper.getString(CursorWrapper.java:114)
12-30 14:37:26.490: E/AndroidRuntime(14558): at com.app.postevery.PostEvery.onActivityResult(PostEvery.java:548)
12-30 14:37:26.490: E/AndroidRuntime(14558): at android.app.Activity.dispatchActivityResult(Activity.java:5423)
12-30 14:37:26.490: E/AndroidRuntime(14558): at android.app.ActivityThread.deliverResults(ActivityThread.java:3361)
12-30 14:37:26.490: E/AndroidRuntime(14558): ... 11 more
答案 0 :(得分:2)
我使用以下代码从sdcard获取图像。此代码在Nexus 5中正常运行。
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
onActivityResult()方法
protected void onActivityResult(final int requestCode,final int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d("Main Activity", "On Activity Result");
try {
Uri selectedImage=null;
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Log.d("Main Activity", "Gallery");
selectedImage = data.getData();
}
if(selectedImage==null)
{
Log.d("Main Activity","Back");
return;
}
Log.d("Main Activity","Out");
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
Log.d("Main Activity",picturePath);
cursor.close();
Bitmap bmp=BitmapFactory.decodeFile(picturePath);
image.setImageBitmap(bmp);
} catch (Exception e) {
// TODO Auto-generated catch block
Log.d("Main Activity","Exception");
}
}
答案 1 :(得分:1)
我已经使用Nexus设备进行了检查。 像
一样启动您的选择器意图Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, ""), GALLERY_PICTURE);
而onActivityResult中的代码是相同的。 您不需要检查nexus设备的SD卡,因为Nexus设备具有模拟Sdcard路径的内部存储。
答案 2 :(得分:0)
这很明显,因为外部存储选项不支持Nexus系列。
因此,首先检查设备是否有SD卡。
Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
if(isSDPresent)
{
// yes SD-card is present
// Fetch from SD card i.e. Path for SD card
// path = getExternalCacheDir().getAbsolutePath() + "/Folder Name";
}
else
{
// path = getFilesDir() + "/Folder Name";
}