Android转换为位图崩溃
好的,所以我应该制作一个android应用程序,但由于某些原因,我无法将我的图片转换为位图图像。它是一个.png图像,当我试图在代码中转换它时,我的应用程序崩溃了,没有错误代码或者什么都没有。我已经试过很多次了,但是我的编程不是很好,我需要帮助,它就是不起作用Android转换为位图崩溃,android,bitmap,crash,uri,Android,Bitmap,Crash,Uri,好的,所以我应该制作一个android应用程序,但由于某些原因,我无法将我的图片转换为位图图像。它是一个.png图像,当我试图在代码中转换它时,我的应用程序崩溃了,没有错误代码或者什么都没有。我已经试过很多次了,但是我的编程不是很好,我需要帮助,它就是不起作用 protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == FOTO
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == FOTO_NEMEN && resultCode == RESULT_OK)
{
final File file = temp;
try {
String urienzo = "file:///sdcard/DCIM/2013-01-30_13-27-28.png";
Uri uri = Uri.parse(urienzo);
Bitmap foto = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
if (foto == null) {
Toast.makeText(this, Uri.fromFile(file).toString(), Toast.LENGTH_SHORT).show();
return;
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
foto.compress(Bitmap.CompressFormat.PNG, 0 , bos);
final byte[] bytes = bos.toByteArray();
bos.close();
AsyncTask<Void,Void,Void> taak = new AsyncTask<Void,Void,Void>() {
@Override
protected Void doInBackground(Void... params) {
stuurAfbeelding(bytes);
return null;
}
};
taak.execute(null,null);
} catch (IOException e) {
Log.e("Snapper","Fout bij foto nemen: " + e);
}
}
}
activityresult上受保护的void(int-requestCode、int-resultCode、Intent-data){
if(requestCode==FOTO\u NEMEN&&resultCode==RESULT\u OK)
{
最终文件=临时文件;
试一试{
字符串urienzo=”file:///sdcard/DCIM/2013-01-30_13-27-28.png";
Uri=Uri.parse(urienzo);
位图foto=MediaStore.Images.Media.getBitmap(this.getContentResolver(),uri);
如果(foto==null){
Toast.makeText(这个,Uri.fromFile(file.toString(),Toast.LENGTH_SHORT).show();
返回;
}
ByteArrayOutputStream bos=新建ByteArrayOutputStream();
压缩(Bitmap.CompressFormat.PNG,0,bos);
最终字节[]字节=bos.toByteArray();
bos.close();
AsyncTask taak=新建AsyncTask(){
@凌驾
受保护的Void doInBackground(Void…参数){
stuurAfbeelding(字节);
返回null;
}
};
taak.execute(null,null);
}捕获(IOE异常){
Log.e(“笛鲷”,“Fout bij foto nemen:”+e);
}
}
}
每当我到达位图foto part时,它就会在没有任何错误消息的情况下崩溃我的应用程序。
我的URI被硬编码的原因是因为我认为URI.from文件给了我错误的URI,所以我想确定一下。现在它崩溃了,我不知道我的代码出了什么问题。有人能帮我吗?我认为你犯了一个错误 要从uri获取位图,您应该使用如下内容:
public static Bitmap getThumbnail(Uri uri) throws FileNotFoundException, IOException{
InputStream input = this.getContentResolver().openInputStream(uri);
BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options();
onlyBoundsOptions.inJustDecodeBounds = true;
onlyBoundsOptions.inDither=true;//optional
onlyBoundsOptions.inPreferredConfig=Bitmap.Config.ARGB_8888;//optional
BitmapFactory.decodeStream(input, null, onlyBoundsOptions);
input.close();
if ((onlyBoundsOptions.outWidth == -1) || (onlyBoundsOptions.outHeight == -1))
return null;
int originalSize = (onlyBoundsOptions.outHeight > onlyBoundsOptions.outWidth) ? onlyBoundsOptions.outHeight : onlyBoundsOptions.outWidth;
double ratio = (originalSize > THUMBNAIL_SIZE) ? (originalSize / THUMBNAIL_SIZE) : 1.0;
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inSampleSize = getPowerOfTwoForSampleRatio(ratio);
bitmapOptions.inDither=true;//optional
bitmapOptions.inPreferredConfig=Bitmap.Config.ARGB_8888;//optional
input = this.getContentResolver().openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(input, null, bitmapOptions);
input.close();
return bitmap;
}
private static int getPowerOfTwoForSampleRatio(double ratio){
int k = Integer.highestOneBit((int)Math.floor(ratio));
if(k==0) return 1;
else return k;
}
其中THUMBNAIL\u SIZE
是您想要获取的缩略图的大小。
因此,它工作正常,我在我的应用程序中使用此代码0
链接您可以这样做:
Bitmap image;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, stream);
bitmap = android.provider.MediaStore.Images.Media.getBitmap(getContentResolver(), intent.getData());
String path = getRealPathFromURI(context, intent.getData());
bitmap = scaleImage(imageView, path);
为了达到目的,你可以尝试以下方法:
Bitmap image;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, stream);
bitmap = android.provider.MediaStore.Images.Media.getBitmap(getContentResolver(), intent.getData());
String path = getRealPathFromURI(context, intent.getData());
bitmap = scaleImage(imageView, path);
在哪里
我想它会崩溃,因为这不是从文件加载图像的方式 代码比您正在尝试的要简单得多:
Bitmap bmp = BitmapFactory.decodeFile(urienzo);
就这些!只要确保这条路是正确的,因为我觉得它不正确
此外,如果您正在加载大图像(例如4MP),它将在内存不足的情况下崩溃,因为位图的概念是用来在屏幕上显示当前大约为HD到FullHD分辨率的内容。检查日志。您是否遇到运行时异常,例如,内存不足异常?@Eneko Lismont:您可以检查我在上回答的类似问题的答案,因此:this关键字在静态语句中无效。当我使用此代码并将其作为方法,并在代码中的同一位置调用它时,它会立即崩溃,通常需要一秒或两秒,现在它会立即崩溃。@Svexo这是因为您没有定义
THUMBNAIL\u SIZE
它不再崩溃!但是位图图像返回为null,并且它显示来自if的错误代码(foto==null)。所以位图部分似乎不再使其崩溃。如我所说,请仔细检查文件路径,因为它似乎不正确。您应该使用environment.getExternalStorageDirectory().getAbsolutePath()+“/DCIM/2013-01-30_13-27-28.png”代码>而不是现在,它再次崩溃,是否有可能图像在某种程度上太大了?有没有可能这不会使它崩溃,但我也有一个异步任务,它试图在后台做一些事情,并得到一个致命的异常?图像分辨率是多少?“只是撞车”还不够好。在Eclipse上有一个名为LogCat的视图,它显示所有日志和所有“崩溃”错误。这里有非常有价值的信息。图片是1280x720,错误是这样的:但我不知道它们是什么意思。还有另一个错误,但它在屏幕外:。对我来说,这意味着我的AsyncTask正在使应用程序崩溃?