从android摄像头捕获图像时,为什么在sd卡文件夹中保存自定义尺寸的图像时使android应用程序崩溃?
在从android摄像头捕获图像后,我尝试用自定义尺寸(宽度x高度)将图像保存在SD卡文件夹中,然后应用程序崩溃 My MainActivity.java如下所示:从android摄像头捕获图像时,为什么在sd卡文件夹中保存自定义尺寸的图像时使android应用程序崩溃?,android,android-camera,image-resizing,android-sdcard,android-camera-intent,Android,Android Camera,Image Resizing,Android Sdcard,Android Camera Intent,在从android摄像头捕获图像后,我尝试用自定义尺寸(宽度x高度)将图像保存在SD卡文件夹中,然后应用程序崩溃 My MainActivity.java如下所示: import com.example.imageresizefromcamera.MainActivity.ScalingUtilities.ScalingLogic; public class MainActivity extends Activity { private static final String TAG
import com.example.imageresizefromcamera.MainActivity.ScalingUtilities.ScalingLogic;
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 0;
public String imageName, imagePath;
File direct;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void captureImage(View v) {
direct = new File(Environment.getExternalStorageDirectory()
+ "/YourFolder");
if (!direct.exists()) {
if (direct.mkdir()) {
// directory is created;
}
}
SecureRandom random = new SecureRandom();
String randomName = new BigInteger(10, random).toString(4);
imageName = "myImage" + "" + randomName + ".JPEG";
File file = new File(direct, 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) {
getScaledBitMap(imagePath, 1536, 2048);
} else if (resultCode == RESULT_CANCELED) {
// user cancelled Image capture
Toast.makeText(getApplicationContext(),
"User cancelled image capture", Toast.LENGTH_SHORT)
.show();
} else {
// failed to capture image
Toast.makeText(getApplicationContext(),
"Sorry! Failed to capture image", Toast.LENGTH_SHORT)
.show();
}
} else {
//
}
}
private Bitmap getScaledBitMap(String filePath, int reqWidth, int reqHeight) {
if (filePath != null && filePath.contains("file")) {
filePath = filePath.replace("file://", "");
}
Bitmap unscaledBitmap = ScalingUtilities.decodeBitmap(getResources(),
filePath, reqWidth, reqHeight, ScalingLogic.CROP);
// Part 2: Scale image
Bitmap scaledBitmap = ScalingUtilities.createScaledBitmap(
unscaledBitmap, reqWidth, reqHeight, ScalingLogic.CROP);
unscaledBitmap.recycle();
// convert and save sd card folder
try {
OutputStream fOut = null;
File file = new File(direct, imageName);
fOut = new FileOutputStream(file);
scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
fOut.flush();
fOut.close();
MediaStore.Images.Media.insertImage(getContentResolver(),
file.getAbsolutePath(), file.getName(), file.getName());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return scaledBitmap;
}
public static class ScalingUtilities {
/**
* Utility function for decoding an image resource. The decoded bitmap
* will be optimized for further scaling to the requested destination
* dimensions and scaling logic.
*
* @param res
* The resources object containing the image data
* @param resId
* The resource id of the image data
* @param dstWidth
* Width of destination area
* @param dstHeight
* Height of destination area
* @param scalingLogic
* Logic to use to avoid image stretching
* @return Decoded bitmap
*/
public static Bitmap decodeBitmap(Resources res, String pathName,
int dstWidth, int dstHeight, ScalingLogic scalingLogic) {
Options options = new Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(pathName, options);
options.inJustDecodeBounds = false;
options.inSampleSize = calculateSampleSize(options.outWidth,
options.outHeight, dstWidth, dstHeight, scalingLogic);
Bitmap unscaledBitmap = BitmapFactory.decodeFile(pathName, options);
return unscaledBitmap;
}
/**
* Utility function for creating a scaled version of an existing bitmap
*
* @param unscaledBitmap
* Bitmap to scale
* @param dstWidth
* Wanted width of destination bitmap
* @param dstHeight
* Wanted height of destination bitmap
* @param scalingLogic
* Logic to use to avoid image stretching
* @return New scaled bitmap object
*/
public static Bitmap createScaledBitmap(Bitmap unscaledBitmap,
int dstWidth, int dstHeight, ScalingLogic scalingLogic) {
Rect srcRect = calculateSrcRect(unscaledBitmap.getWidth(),
unscaledBitmap.getHeight(), dstWidth, dstHeight,
scalingLogic);
Rect dstRect = calculateDstRect(unscaledBitmap.getWidth(),
unscaledBitmap.getHeight(), dstWidth, dstHeight,
scalingLogic);
Bitmap scaledBitmap = Bitmap.createBitmap(dstRect.width(),
dstRect.height(), Config.ARGB_8888);
Canvas canvas = new Canvas(scaledBitmap);
canvas.drawBitmap(unscaledBitmap, srcRect, dstRect, new Paint(
Paint.FILTER_BITMAP_FLAG));
return scaledBitmap;
}
public static enum ScalingLogic {
CROP, FIT
}
public static int calculateSampleSize(int srcWidth, int srcHeight,
int dstWidth, int dstHeight, ScalingLogic scalingLogic) {
if (scalingLogic == ScalingLogic.FIT) {
final float srcAspect = (float) srcWidth / (float) srcHeight;
final float dstAspect = (float) dstWidth / (float) dstHeight;
if (srcAspect > dstAspect) {
return srcWidth / dstWidth;
} else {
return srcHeight / dstHeight;
}
} else {
final float srcAspect = (float) srcWidth / (float) srcHeight;
final float dstAspect = (float) dstWidth / (float) dstHeight;
if (srcAspect > dstAspect) {
return srcHeight / dstHeight;
} else {
return srcWidth / dstWidth;
}
}
}
public static Rect calculateSrcRect(int srcWidth, int srcHeight,
int dstWidth, int dstHeight, ScalingLogic scalingLogic) {
if (scalingLogic == ScalingLogic.CROP) {
final float srcAspect = (float) srcWidth / (float) srcHeight;
final float dstAspect = (float) dstWidth / (float) dstHeight;
if (srcAspect > dstAspect) {
final int srcRectWidth = (int) (srcHeight * dstAspect);
final int srcRectLeft = (srcWidth - srcRectWidth) / 2;
return new Rect(srcRectLeft, 0, srcRectLeft + srcRectWidth,
srcHeight);
} else {
final int srcRectHeight = (int) (srcWidth / dstAspect);
final int scrRectTop = (int) (srcHeight - srcRectHeight) / 2;
return new Rect(0, scrRectTop, srcWidth, scrRectTop
+ srcRectHeight);
}
} else {
return new Rect(0, 0, srcWidth, srcHeight);
}
}
public static Rect calculateDstRect(int srcWidth, int srcHeight,
int dstWidth, int dstHeight, ScalingLogic scalingLogic) {
if (scalingLogic == ScalingLogic.FIT) {
final float srcAspect = (float) srcWidth / (float) srcHeight;
final float dstAspect = (float) dstWidth / (float) dstHeight;
if (srcAspect > dstAspect) {
return new Rect(0, 0, dstWidth,
(int) (dstWidth / srcAspect));
} else {
return new Rect(0, 0, (int) (dstHeight * srcAspect),
dstHeight);
}
} else {
return new Rect(0, 0, dstWidth, dstHeight);
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.imageresizefromcamera"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.imageresizefromcamera.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
下面是我的AndroidMenifest.xml:
import com.example.imageresizefromcamera.MainActivity.ScalingUtilities.ScalingLogic;
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 0;
public String imageName, imagePath;
File direct;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void captureImage(View v) {
direct = new File(Environment.getExternalStorageDirectory()
+ "/YourFolder");
if (!direct.exists()) {
if (direct.mkdir()) {
// directory is created;
}
}
SecureRandom random = new SecureRandom();
String randomName = new BigInteger(10, random).toString(4);
imageName = "myImage" + "" + randomName + ".JPEG";
File file = new File(direct, 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) {
getScaledBitMap(imagePath, 1536, 2048);
} else if (resultCode == RESULT_CANCELED) {
// user cancelled Image capture
Toast.makeText(getApplicationContext(),
"User cancelled image capture", Toast.LENGTH_SHORT)
.show();
} else {
// failed to capture image
Toast.makeText(getApplicationContext(),
"Sorry! Failed to capture image", Toast.LENGTH_SHORT)
.show();
}
} else {
//
}
}
private Bitmap getScaledBitMap(String filePath, int reqWidth, int reqHeight) {
if (filePath != null && filePath.contains("file")) {
filePath = filePath.replace("file://", "");
}
Bitmap unscaledBitmap = ScalingUtilities.decodeBitmap(getResources(),
filePath, reqWidth, reqHeight, ScalingLogic.CROP);
// Part 2: Scale image
Bitmap scaledBitmap = ScalingUtilities.createScaledBitmap(
unscaledBitmap, reqWidth, reqHeight, ScalingLogic.CROP);
unscaledBitmap.recycle();
// convert and save sd card folder
try {
OutputStream fOut = null;
File file = new File(direct, imageName);
fOut = new FileOutputStream(file);
scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
fOut.flush();
fOut.close();
MediaStore.Images.Media.insertImage(getContentResolver(),
file.getAbsolutePath(), file.getName(), file.getName());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return scaledBitmap;
}
public static class ScalingUtilities {
/**
* Utility function for decoding an image resource. The decoded bitmap
* will be optimized for further scaling to the requested destination
* dimensions and scaling logic.
*
* @param res
* The resources object containing the image data
* @param resId
* The resource id of the image data
* @param dstWidth
* Width of destination area
* @param dstHeight
* Height of destination area
* @param scalingLogic
* Logic to use to avoid image stretching
* @return Decoded bitmap
*/
public static Bitmap decodeBitmap(Resources res, String pathName,
int dstWidth, int dstHeight, ScalingLogic scalingLogic) {
Options options = new Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(pathName, options);
options.inJustDecodeBounds = false;
options.inSampleSize = calculateSampleSize(options.outWidth,
options.outHeight, dstWidth, dstHeight, scalingLogic);
Bitmap unscaledBitmap = BitmapFactory.decodeFile(pathName, options);
return unscaledBitmap;
}
/**
* Utility function for creating a scaled version of an existing bitmap
*
* @param unscaledBitmap
* Bitmap to scale
* @param dstWidth
* Wanted width of destination bitmap
* @param dstHeight
* Wanted height of destination bitmap
* @param scalingLogic
* Logic to use to avoid image stretching
* @return New scaled bitmap object
*/
public static Bitmap createScaledBitmap(Bitmap unscaledBitmap,
int dstWidth, int dstHeight, ScalingLogic scalingLogic) {
Rect srcRect = calculateSrcRect(unscaledBitmap.getWidth(),
unscaledBitmap.getHeight(), dstWidth, dstHeight,
scalingLogic);
Rect dstRect = calculateDstRect(unscaledBitmap.getWidth(),
unscaledBitmap.getHeight(), dstWidth, dstHeight,
scalingLogic);
Bitmap scaledBitmap = Bitmap.createBitmap(dstRect.width(),
dstRect.height(), Config.ARGB_8888);
Canvas canvas = new Canvas(scaledBitmap);
canvas.drawBitmap(unscaledBitmap, srcRect, dstRect, new Paint(
Paint.FILTER_BITMAP_FLAG));
return scaledBitmap;
}
public static enum ScalingLogic {
CROP, FIT
}
public static int calculateSampleSize(int srcWidth, int srcHeight,
int dstWidth, int dstHeight, ScalingLogic scalingLogic) {
if (scalingLogic == ScalingLogic.FIT) {
final float srcAspect = (float) srcWidth / (float) srcHeight;
final float dstAspect = (float) dstWidth / (float) dstHeight;
if (srcAspect > dstAspect) {
return srcWidth / dstWidth;
} else {
return srcHeight / dstHeight;
}
} else {
final float srcAspect = (float) srcWidth / (float) srcHeight;
final float dstAspect = (float) dstWidth / (float) dstHeight;
if (srcAspect > dstAspect) {
return srcHeight / dstHeight;
} else {
return srcWidth / dstWidth;
}
}
}
public static Rect calculateSrcRect(int srcWidth, int srcHeight,
int dstWidth, int dstHeight, ScalingLogic scalingLogic) {
if (scalingLogic == ScalingLogic.CROP) {
final float srcAspect = (float) srcWidth / (float) srcHeight;
final float dstAspect = (float) dstWidth / (float) dstHeight;
if (srcAspect > dstAspect) {
final int srcRectWidth = (int) (srcHeight * dstAspect);
final int srcRectLeft = (srcWidth - srcRectWidth) / 2;
return new Rect(srcRectLeft, 0, srcRectLeft + srcRectWidth,
srcHeight);
} else {
final int srcRectHeight = (int) (srcWidth / dstAspect);
final int scrRectTop = (int) (srcHeight - srcRectHeight) / 2;
return new Rect(0, scrRectTop, srcWidth, scrRectTop
+ srcRectHeight);
}
} else {
return new Rect(0, 0, srcWidth, srcHeight);
}
}
public static Rect calculateDstRect(int srcWidth, int srcHeight,
int dstWidth, int dstHeight, ScalingLogic scalingLogic) {
if (scalingLogic == ScalingLogic.FIT) {
final float srcAspect = (float) srcWidth / (float) srcHeight;
final float dstAspect = (float) dstWidth / (float) dstHeight;
if (srcAspect > dstAspect) {
return new Rect(0, 0, dstWidth,
(int) (dstWidth / srcAspect));
} else {
return new Rect(0, 0, (int) (dstHeight * srcAspect),
dstHeight);
}
} else {
return new Rect(0, 0, dstWidth, dstHeight);
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.imageresizefromcamera"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.imageresizefromcamera.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
实际上我想要一些东西,比如
1.从android摄像头拍摄图像
2.调整自定义大小(1536 X 2048)
三,。最后保存在sd卡文件夹中(1536 X 2048)
任何人都请帮助我。如果我的程序错误,您可以编辑我的问题/代码。但答案对我来说是至关重要的。请向我们展示stacktraceplease检查代码是否允许在manifestfile写入中存储external@sania,需要许可证您正在sd卡号中保存