因此,由于仅从 android.media.action.IMAGE_CAPTURE 获取数据的质量问题,我显然需要调用活动,保存一个本地临时文件,然后我可以从中创建位图。这个Bitmap会被裁剪成一个正方形,设置成一个ImageView,然后存储在本地;但是,我遇到了一些问题。一切(现在不包括裁剪)都可以设置图像(它返回一个空白图像)并将其存储在内部,此时它会崩溃。我尝试使用文件管理器在 SD 卡上查找文件,但该文件夹甚至没有显示,所以我不知道该文件是否真的被创建。任何想法我做错了什么?编码:
private static final String TAG = "AddEdit";
private final static String FOLDER_NAME = "com.ex.beerlog/Image/";
private Uri selectedImageUri = null;
private final static int CAPTURE_IMAGE_CALLBACK = 1;
switch (arg0.getId()) {
case R.id.imageView1:
File photo = null;
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
photo = new File(android.os.Environment.getExternalStorageDirectory(), FOLDER_NAME + File.separator
+ "temp.png");
Log.w(TAG, "*******Photo path set from Enviro*********");
} else {
photo = new File(getCacheDir(), FOLDER_NAME + File.separator + "temp.png");
Log.w(TAG, "*******photo set from cache*********");
}
if (photo != null) {
Log.w(TAG, "*******Photo is not null*********");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
selectedImageUri = Uri.fromFile(photo);
Log.w(TAG, "*******from imageUri=" + selectedImageUri + "*********");
startActivityForResult(intent, CAPTURE_IMAGE_CALLBACK);
}
break;
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case CAPTURE_IMAGE_CALLBACK:
if (resultCode == RESULT_OK) {
String path = selectedImageUri.toString();
Log.w(TAG, "*****path set =" + path + "******");
BitmapFactory.Options options = new BitmapFactory.Options();
Log.w(TAG, "*****Bimapfactory options set******");
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Log.w(TAG, "****Options defined********");
bmp = BitmapFactory.decodeFile(path, options);
Log.w(TAG, "*****Bmp is set ==> crop******");
brewPhoto.setImageBitmap(bmp);
Log.w(TAG, "******************Image is set to ButtonImage**********************");
// Store image
FileOutputStream fos;
Log.w(TAG, "********FileOutPutStream created*************");
try {
fos = openFileOutput(cDate, Context.MODE_PRIVATE);
Log.w(TAG, "********FileOutPutStream defined*************");
bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
Log.w(TAG, "********bitmap compressed*************");
fos.close();
Log.w(TAG, "*****FOS closed**********");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
break;
}
}
这是我收到的错误日志:
03-18 21:24:50.325: W/AddEdit(8208): *****path set =file:///storage/sdcard0/com.ex.beerlog/Image/temp.png******
03-18 21:24:50.325: W/AddEdit(8208): *****Bimapfactory options set******
03-18 21:24:50.325: W/AddEdit(8208): ****Options defined********
03-18 21:24:50.325: I/System.out(8208): Not a DRM File, opening notmally
03-18 21:24:50.325: W/AddEdit(8208): *****Bmp is set ==> crop******
03-18 21:24:50.325: W/AddEdit(8208): ******************Image is set to ButtonImage**********************
03-18 21:24:50.325: W/AddEdit(8208): ********FileOutPutStream created*************
03-18 21:24:50.325: W/AddEdit(8208): ********FileOutPutStream defined*************
03-18 21:24:50.335: W/dalvikvm(8208): threadid=1: thread exiting with uncaught exception (group=0x41314438)
03-18 21:24:50.335: E/AndroidRuntime(8208): FATAL EXCEPTION: main
03-18 21:24:50.335: E/AndroidRuntime(8208): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.ex.beerlog/com.ex.beerlog.AddEdit}: java.lang.NullPointerException
03-18 21:24:50.335: E/AndroidRuntime(8208): at android.app.ActivityThread.deliverResults(ActivityThread.java:3183)
03-18 21:24:50.335: E/AndroidRuntime(8208): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3226)
03-18 21:24:50.335: E/AndroidRuntime(8208): at android.app.ActivityThread.access$1200(ActivityThread.java:139)
03-18 21:24:50.335: E/AndroidRuntime(8208): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1272)
03-18 21:24:50.335: E/AndroidRuntime(8208): at android.os.Handler.dispatchMessage(Handler.java:99)
03-18 21:24:50.335: E/AndroidRuntime(8208): at android.os.Looper.loop(Looper.java:137)
03-18 21:24:50.335: E/AndroidRuntime(8208): at android.app.ActivityThread.main(ActivityThread.java:4918)
03-18 21:24:50.335: E/AndroidRuntime(8208): at java.lang.reflect.Method.invokeNative(Native Method)
03-18 21:24:50.335: E/AndroidRuntime(8208): at java.lang.reflect.Method.invoke(Method.java:511)
03-18 21:24:50.335: E/AndroidRuntime(8208): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
03-18 21:24:50.335: E/AndroidRuntime(8208): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
03-18 21:24:50.335: E/AndroidRuntime(8208): at dalvik.system.NativeStart.main(Native Method)
03-18 21:24:50.335: E/AndroidRuntime(8208): Caused by: java.lang.NullPointerException
03-18 21:24:50.335: E/AndroidRuntime(8208): at com.ex.beerlog.AddEdit.onActivityResult(AddEdit.java:329)
03-18 21:24:50.335: E/AndroidRuntime(8208): at android.app.Activity.dispatchActivityResult(Activity.java:5232)
03-18 21:24:50.335: E/AndroidRuntime(8208): at android.app.ActivityThread.deliverResults(ActivityThread.java:3179)
03-18 21:24:50.335: E/AndroidRuntime(8208): ... 11 more
如您所见,fileInputSteam 已创建,但在尝试存储图像时崩溃。我已经删除了这段代码,并且设置为 ImageView 的图像是空白的(透明的)。所以我假设它崩溃了,因为实际上什么都没有?我试图在清单中设置权限以允许访问 SD 卡。另请注意,我正在我的 Galaxy sIII 上进行测试,并启用了调试器。我还取消了 SD 卡的保护并拔下手机以确保 SD 卡可用,但每次都是相同的结果。
再次感谢。你们这里的人都非常擅长这些东西!