我是堆栈溢出的新手,所以如果我做错了,请提前道歉:)。
我试图创建一个从sdCard返回图像的意图,然后我将使用BitmapFactory来改变革命。但是我的意图是没有返回正确的文件名。我已经提供了下面的错误日志以及创建意图以及我想要对结果做什么(onActivityResult())。
错误日志:
01-10 18:51:34.043: E/BitmapFactory(15528): Unable to decode stream: java.io.FileNotFoundException: /IMG-20160110-WA0001.jpg: open failed: ENOENT (No such file or directory)
01-10 18:51:34.044: E/BitmapFactory(15528): Unable to decode stream: java.io.FileNotFoundException: /IMG-20160110-WA0001.jpg: open failed: ENOENT (No such file or directory)
01-10 18:51:34.046: E/AndroidRuntime(15528): Process: com.example.george.soundboard, PID: 15528
01-10 18:51:34.046: E/AndroidRuntime(15528): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/image:38261 flg=0x1 }} to activity {com.example.george.soundboard/com.example.george.soundboard.ChooseElement}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
01-10 18:51:34.046: E/AndroidRuntime(15528): at android.app.ActivityThread.deliverResults(ActivityThread.java:3699)
01-10 18:51:34.046: E/AndroidRuntime(15528): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
01-10 18:51:34.046: E/AndroidRuntime(15528): at android.app.ActivityThread.-wrap16(ActivityThread.java)
01-10 18:51:34.046: E/AndroidRuntime(15528): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
01-10 18:51:34.046: E/AndroidRuntime(15528): at android.os.Handler.dispatchMessage(Handler.java:102)
01-10 18:51:34.046: E/AndroidRuntime(15528): at android.os.Looper.loop(Looper.java:148)
01-10 18:51:34.046: E/AndroidRuntime(15528): at android.app.ActivityThread.main(ActivityThread.java:5417)
01-10 18:51:34.046: E/AndroidRuntime(15528): at java.lang.reflect.Method.invoke(Native Method)
01-10 18:51:34.046: E/AndroidRuntime(15528): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
01-10 18:51:34.046: E/AndroidRuntime(15528): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
01-10 18:51:34.046: E/AndroidRuntime(15528): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
01-10 18:51:34.046: E/AndroidRuntime(15528): at com.example.george.soundboard.ChooseElement.onActivityResult(ChooseElement.java:210)
01-10 18:51:34.046: E/AndroidRuntime(15528): at android.app.Activity.dispatchActivityResult(Activity.java:6456)
01-10 18:51:34.046: E/AndroidRuntime(15528): at android.app.ActivityThread.deliverResults(ActivityThread.java:3695)
创造意图:
Intent pickIntent = new Intent();
pickIntent.setType("image/*");
pickIntent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(pickIntent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
onActivityResult():
File i = new File(getFileName(data.getData()));
SetActivity.bluePrint[SetActivity.id].image = decodeImage(i, 100, 100);
image.setImageBitmap(SetActivity.bluePrint[SetActivity.id].image);
public String getFileName(Uri uri) {
String result = null;
if (uri.getScheme().equals("content")) {
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
try {
if (cursor != null && cursor.moveToFirst()) {
result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
}
} finally {
cursor.close();
}
}
if (result == null) {
result = uri.getPath();
int cut = result.lastIndexOf('/');
if (cut != -1) {
result = result.substring(cut + 1);
}
}
return result;
}
public static Bitmap decodeImage(File file, int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(file.getAbsolutePath(), options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(file.getAbsolutePath(), options);
}
提前感谢您的帮助:)当然,如果还有其他信息,请询问。
答案 0 :(得分:0)
您的getFileName()
方法无用(因为您不需要它)而且错误(因为DISPLAY_NAME
没有返回路径)。使用ContentResolver
和openInputStream()
在InputStream
指向的内容上打开Uri
。将其传递给decodeStream()
上的BitmapFactory
。
更好的是,摆脱大部分代码,并使用像Picasso这样的库,用ImageView
异步填充Uri
的图像。