我在我的應用程序中有一個功能,我打開照片的Galery並選擇要在ImageView中顯示的圖像。奇怪的是,在我的三星Galaxy S5上,代碼工作完美,但是當我嘗試使用華碩Zenphone或摩托羅拉Moto X時,代碼無法正常工作,應用程序崩潰。
所有設備具有相同的Android版本(5.0)
這裏是我的代碼示例:
意圖打開照片的galery:Android從照片的Galery獲取圖片 - 在某些設備上出錯
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, IMG_SDCARD);
活動結果:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
File file = null;
if(data != null && requestCode == IMG_SDCARD && resultCode == getActivity().RESULT_OK){
Uri img = data.getData();
String[] cols = { MediaStore.Images.Media.DATA };
Cursor cursor = getActivity().getContentResolver().query(img, cols, null, null, null);
cursor.moveToFirst();
int indexCol = cursor.getColumnIndex(cols[0]);
String imgString = cursor.getString(indexCol);
cursor.close();
file = new File(imgString);
if(file != null){
wd.getImage().setResizedBitmap(file, 1200, 700);
wd.getImage().setMimeFromImgPath(file.getPath());
}
}
if(wd.getImage().getBitmap() != null){
imageView.setImageBitmap(wd.getImage().getBitmap());
}
}
你們有沒有看到這樣的事情?
在不同設備之間從照片的Galery獲取圖像可能有區別嗎?
謝謝你們
編輯
這是日誌錯誤:
01-21 10:34:01.066 30243-30243/com.inthecheesefactory.lab.designlibrary E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.inthecheesefactory.lab.designlibrary, PID: 30243
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65539, result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/image:4904 flg=0x1 }} to activity {com.inthecheesefactory.lab.designlibrary/br.com.amais.viumeupet.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'char[] java.lang.String.toCharArray()' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:3559)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3602)
at android.app.ActivityThread.access$1300(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1334)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5289)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'char[] java.lang.String.toCharArray()' on a null object reference
at java.io.File.fixSlashes(File.java:179)
at java.io.File.<init>(File.java:128)
at br.com.amais.viumeupet.Fragment1.onActivityResult(Fragment1.java:285)
at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:153)
at br.com.amais.viumeupet.MainActivity.onActivityResult(MainActivity.java:231)
at android.app.Activity.dispatchActivityResult(Activity.java:6220)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3555)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3602)
at android.app.ActivityThread.access$1300(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1334)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5289)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
你有你的錯誤日誌? –
http://stackoverflow.com/questions/33938665/android-integrating-gallery-functionality-with-camera-capture/33939277#33939277 –
發佈錯誤日誌。 –