我的代码是: MainActivity.java:
public class MainActivity extends ActionBarActivity {
private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 0;
public String imageName = null, imagePath;
public ImageView imageView1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView1 = (ImageView) findViewById(R.id.imageView1);
SecureRandom random = new SecureRandom();
String randomName = new BigInteger(10, random).toString(4);
imageName = "myImage" + "" + randomName + ".JPEG";
File makeFile = new File(Environment.getExternalStorageDirectory()
+ "/" + "imageFolderRoted");
if (!makeFile.exists()) {
if (makeFile.mkdir()) {
}
}
File file = new File(makeFile, imageName);
imagePath = file.getAbsolutePath();
Uri outputFileUri = Uri.fromFile(file);
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(i, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Matrix matrix = new Matrix();
matrix.postRotate(getImageOrientation(imagePath));
Bitmap bitmap = getPreview(imagePath);
Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
bitmap.getWidth(), bitmap.getHeight(), matrix, true);
imageView1.setImageBitmap(rotatedBitmap);
} else if (resultCode == RESULT_CANCELED) {
// user cancelled Image capture
}
}
}
public static int getImageOrientation(String imagePath) {
int rotate = 0;
try {
File imageFile = new File(imagePath);
ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
}
} catch (IOException e) {
e.printStackTrace();
}
return rotate;
}
public Bitmap getPreview(String fileName) {
File image = new File(fileName);
BitmapFactory.Options bounds = new BitmapFactory.Options();
bounds.inJustDecodeBounds = true;
BitmapFactory.decodeFile(image.getPath(), bounds);
if ((bounds.outWidth == -1) || (bounds.outHeight == -1)) {
return null;
}
int originalSize = (bounds.outHeight > bounds.outWidth) ? bounds.outHeight
: bounds.outWidth;
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = 8;
return BitmapFactory.decodeFile(image.getPath(), opts);
}
}
在AndroidMenifest.xml中添加权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
运行时(横向)然后应用程序崩溃。并且还显示不幸的是,图像没有停止。
如何从横向模式成功捕获图像。
[N.B]:在设备Samsung s4中运行
然后显示logcat崩溃消息:
AndroidRuntime(4002): FATAL EXCEPTION: main
05-25 10:53:14.232: E/AndroidRuntime(4002): java.lang.RuntimeException: Unable to resume activity {com.example.imagenotroted/com.example.imagenotroted.MainActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=null} to activity {com.example.imagenotroted/com.example.imagenotroted.MainActivity}: java.lang.NullPointerException
05-25 10:53:14.232: E/AndroidRuntime(4002): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2595)
05-25 10:53:14.232: E/AndroidRuntime(4002): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2623)
05-25 10:53:14.232: E/AndroidRuntime(4002): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2109)
05-25 10:53:14.232: E/AndroidRuntime(4002): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3510)
05-25 10:53:14.232: E/AndroidRuntime(4002): at android.app.ActivityThread.access$700(ActivityThread.java:134)
05-25 10:53:14.232: E/AndroidRuntime(4002): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1251)
05-25 10:53:14.232: E/AndroidRuntime(4002): at android.os.Handler.dispatchMessage(Handler.java:99)
05-25 10:53:14.232: E/AndroidRuntime(4002): at android.os.Looper.loop(Looper.java:154)
05-25 10:53:14.232: E/AndroidRuntime(4002): at android.app.ActivityThread.main(ActivityThread.java:4624)
05-25 10:53:14.232: E/AndroidRuntime(4002): at java.lang.reflect.Method.invokeNative(Native Method)
05-25 10:53:14.232: E/AndroidRuntime(4002): at java.lang.reflect.Method.invoke(Method.java:511)
05-25 10:53:14.232: E/AndroidRuntime(4002): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
05-25 10:53:14.232: E/AndroidRuntime(4002): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
05-25 10:53:14.232: E/AndroidRuntime(4002): at dalvik.system.NativeStart.main(Native Method)
05-25 10:53:14.232: E/AndroidRuntime(4002): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=null} to activity {com.example.imagenotroted/com.example.imagenotroted.MainActivity}: java.lang.NullPointerException
05-25 10:53:14.232: E/AndroidRuntime(4002): at android.app.ActivityThread.deliverResults(ActivityThread.java:3135)
05-25 10:53:14.232: E/AndroidRuntime(4002): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2578)
05-25 10:53:14.232: E/AndroidRuntime(4002): ... 13 more
05-25 10:53:14.232: E/AndroidRuntime(4002): Caused by: java.lang.NullPointerException
05-25 10:53:14.232: E/AndroidRuntime(4002): at com.example.imagenotroted.MainActivity.onActivityResult(MainActivity.java:63)
05-25 10:53:14.232: E/AndroidRuntime(4002): at android.app.Activity.dispatchActivityResult(Activity.java:4730)
05-25 10:53:14.232: E/AndroidRuntime(4002): at android.app.ActivityThread.deliverResults(ActivityThread.java:3131)
05-25 10:53:14.232: E/AndroidRuntime(4002): ... 14 more
05-25 10:53:15.279: E/ActivityManager(234): mtprof entry can not found!
05-25 10:53:15.279: E/ActivityManager(234): java.io.FileNotFoundException: /proc/mtprof/status: open failed: ENOENT (No such file or directory)
05-25 10:53:15.279: E/ActivityManager(234): at libcore.io.IoBridge.open(IoBridge.java:448)
05-25 10:53:15.279: E/ActivityManager(234): at java.io.FileInputStream.<init>(FileInputStream.java:78)
05-25 10:53:15.279: E/ActivityManager(234): at java.io.FileInputStream.<init>(FileInputStream.java:105)
05-25 10:53:15.279: E/ActivityManager(234): at com.android.server.am.ActivityRecord.mtProf(ActivityRecord.java:872)
05-25 10:53:15.279: E/ActivityManager(234): at com.android.server.am.ActivityRecord.windowsDrawn(ActivityRecord.java:662)
05-25 10:53:15.279: E/ActivityManager(234): at com.android.server.am.ActivityRecord$Token.windowsDrawn(ActivityRecord.java:225)
05-25 10:53:15.279: E/ActivityManager(234): at com.android.server.wm.WindowManagerService$H.handleMessage(WindowManagerService.java:7056)
05-25 10:53:15.279: E/ActivityManager(234): at android.os.Handler.dispatchMessage(Handler.java:99)
05-25 10:53:15.279: E/ActivityManager(234): at android.os.Looper.loop(Looper.java:154)
05-25 10:53:15.279: E/ActivityManager(234): at com.android.server.wm.WindowManagerService$WMThread.run(WindowManagerService.java:758)
05-25 10:53:15.279: E/ActivityManager(234): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
05-25 10:53:15.279: E/ActivityManager(234): at libcore.io.Posix.open(Native Method)
05-25 10:53:15.279: E/ActivityManager(234): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
05-25 10:53:15.279: E/ActivityManager(234): at libcore.io.IoBridge.open(IoBridge.java:432)
05-25 10:53:15.279: E/ActivityManager(234): ... 9 more
05-25 10:53:19.025: E/Activity(3652): zbx The class is: android.process.media