捕获图像崩溃的HTC设备 [英] Capturing images crashing on HTC devices
问题描述
您好我正在开发一个应用程序,捕捉图像,并通过电子邮件发送捕捉的图像正在三星galaxy和索尼爱立信XPERIA不错,但它不工作的HTC设备任何人都知道一个道理,为什么?这里是我的code拍摄照片
{尝试
JPG字符串文件名= IMAGE_NAME +;
//创建与文件名参数意向
ContentValues值=新ContentValues();
values.put(MediaStore.Images.Media.TITLE,文件名);
values.put(MediaStore.Images.Media.DESCRIPTION,由摄像机图像捕捉);
// imageUri是当前活动属性,定义并保存起来以备以后使用(也的onSaveInstanceState)
outuri = getContentResolver()。插入(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,价值观);
意图cameraIntent =新意图(MediaStore.ACTION_IMAGE_CAPTURE);
// outuri = Uri.fromFile(照片);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,outuri);
cameraIntent.putExtra(回归数据,真正的);
startActivityForResult(cameraIntent,2);
}赶上(例外五){
Toast.makeText(preview.this,+ E,Toast.LENGTH_LONG).show();
}
和这里是我用来检索图像code
路径= convertImageUriToFile(outuri,preview.this).getAbsolutePath();
arr.add(路径);
尝试{
位=的getImage(路径);
公共静态文件convertImageUriToFile(URI imageUri,活动活动){
光标光标= NULL;
尝试{
的String [] = PROJ {MediaStore.Images.Media.DATA,MediaStore.Images.Media._ID,MediaStore.Images.ImageColumns.ORIENTATION};
光标= activity.managedQuery(imageUri,凸出,//哪些列返回
空,// WHERE子句;该行返回(所有行)
空,// WHERE子句选择参数(无)
空值); //订单by子句(按名称升序)
INT file_ColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
INT orientation_ColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.ORIENTATION);
如果(cursor.moveToFirst()){
字符串的方向= cursor.getString(orientation_ColumnIndex);
返回新的文件(cursor.getString(file_ColumnIndex));
}
返回null;
} {最后
如果(指针!= NULL){
cursor.close();
}
}
} 公共位图的getImage(字符串路径)抛出IOException异常
{
BitmapFactory.Options选项=新BitmapFactory.Options();
options.inJustDe codeBounds = TRUE;
BitmapFactory.de codeFILE(路径选择);
位图targetBitmap = NULL;
INT srcWidth = options.outWidth;
INT srcHeight = options.outHeight;
INT [] newWH =新INT [2];
newWH [0] = 1024;
newWH [1] =(1024 * srcHeight)/ srcWidth; INT inSampleSize = 1;
而(srcWidth / 2 - ; newWH [0]){
srcWidth / = 2;
srcHeight / = 2;
inSampleSize * = 2;
}
//浮动desiredScale =(浮点)newWH [0] / srcWidth;
//德code。与inSampleSize
options.inJustDe codeBounds = FALSE;
options.inDither = FALSE;
options.inSampleSize = inSampleSize;
options.inScaled = FALSE;
options.in preferredConfig = Bitmap.Config.ARGB_8888;
位图sampledSrcBitmap = BitmapFactory.de codeFILE(路径选择);
ExifInterface EXIF =新ExifInterface(路径);
字符串s = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
System.out.println(\"Orientation>>>>>>>>>>>>>>>>>>>>\"+s);
字模=新的Matrix();
浮动旋转= rotationForImage(preview.this,Uri.fromFile(新文件(路径)));
如果(旋转!= 0F){
。矩阵preRotate(旋转);
}
INT newh =(W * sampledSrcBitmap.getHeight())/sampledSrcBitmap.getWidth();
位图R = Bitmap.createScaledBitmap(sampledSrcBitmap,W,newh,真正的);
位图resizedBitmap = Bitmap.createBitmap(
R,0,0,W,newh,矩阵,真); 返回resizedBitmap;
}
}
很好,有一个在 Intent.putExtra(MediaStore.EXTRA_OUTPUT)一个已知的bug
键,它会导致崩溃的应用程序。
退房问同样的问题时,我得到了答案:<一href=\"http://stackoverflow.com/a/10613299/1056359\">http://stackoverflow.com/a/10613299/1056359
Hi I am developing a app that captures images and email it capturing the images is working fine on Samsung galaxy and Sony Ericsson xperia but it's not working on HTC devices anyone know a reason why ?? here is my code for capturing images
try {
String fileName = Image_name+".jpg";
//create parameters for Intent with filename
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
values.put(MediaStore.Images.Media.DESCRIPTION,"Image capture by camera");
//imageUri is the current activity attribute, define and save it for later usage (also in onSaveInstanceState)
outuri = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//outuri = Uri.fromFile(photo);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outuri);
cameraIntent.putExtra("return-data", true);
startActivityForResult(cameraIntent, 2);
} catch (Exception e) {
Toast.makeText(preview.this, ""+e, Toast.LENGTH_LONG).show();
}
and here is code that I use to retrieve images
path = convertImageUriToFile(outuri, preview.this).getAbsolutePath();
arr.add(path);
try {
bitmap = getImage(path);
public static File convertImageUriToFile (Uri imageUri, Activity activity) {
Cursor cursor = null;
try {
String [] proj={MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID, MediaStore.Images.ImageColumns.ORIENTATION};
cursor = activity.managedQuery(imageUri, proj, // Which columns to return
null, // WHERE clause; which rows to return (all rows)
null, // WHERE clause selection arguments (none)
null); // Order-by clause (ascending by name)
int file_ColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
int orientation_ColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.ORIENTATION);
if (cursor.moveToFirst()) {
String orientation = cursor.getString(orientation_ColumnIndex);
return new File(cursor.getString(file_ColumnIndex));
}
return null;
} finally {
if (cursor != null) {
cursor.close();
}
}
}
public Bitmap getImage(String path) throws IOException
{
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
Bitmap targetBitmap=null;
int srcWidth = options.outWidth;
int srcHeight = options.outHeight;
int[] newWH = new int[2];
newWH[0] = 1024;
newWH[1] = (1024*srcHeight)/srcWidth;
int inSampleSize = 1;
while(srcWidth / 2 > newWH[0]){
srcWidth /= 2;
srcHeight /= 2;
inSampleSize *= 2;
}
// float desiredScale = (float) newWH[0] / srcWidth;
// Decode with inSampleSize
options.inJustDecodeBounds = false;
options.inDither = false;
options.inSampleSize = inSampleSize;
options.inScaled = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap sampledSrcBitmap = BitmapFactory.decodeFile(path,options);
ExifInterface exif = new ExifInterface(path);
String s=exif.getAttribute(ExifInterface.TAG_ORIENTATION);
System.out.println("Orientation>>>>>>>>>>>>>>>>>>>>"+s);
Matrix matrix = new Matrix();
float rotation = rotationForImage(preview.this, Uri.fromFile(new File(path)));
if (rotation != 0f) {
matrix.preRotate(rotation);
}
int newh = ( w * sampledSrcBitmap.getHeight() ) /sampledSrcBitmap.getWidth();
Bitmap r=Bitmap.createScaledBitmap(sampledSrcBitmap, w, newh, true);
Bitmap resizedBitmap = Bitmap.createBitmap(
r, 0, 0, w, newh, matrix, true);
return resizedBitmap;
}
}
well, there's a known bug in the Intent.putExtra(MediaStore.EXTRA_OUTPUT)
and it causes a crash in the app.
check out the answer I got when asking the same question: http://stackoverflow.com/a/10613299/1056359
这篇关于捕获图像崩溃的HTC设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!