0

好的,所以我应该制作一个 android 应用程序,但由于某种原因,我无法将我的图片转换为位图图像。这是一个 .png 图像,当我尝试在我的代码中转换它时,我的应用程序崩溃了,没有错误代码或什么也没有。我已经尝试修复它很多次了,但我只是在编程方面不是那么好,我需要帮助,它就是行不通。

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);
                    }
                }
            }

每当我到达位图照片部分时,它都会使我的应用程序崩溃而没有任何错误消息。我的 URI 被硬编码的原因是因为我认为 URI.fromfile 给了我错误的 URI,所以我想确定一下。现在它只是崩溃了,我不知道我的代码有什么问题。有人可以帮助我吗?

4

3 回答 3

1

在我看来,你会得到一个 outOfMemoryError。

要从 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_SIZE你想得到的缩略图的大小在哪里。所以,它工作正常,我在我的应用程序中使用此代码0

链接如何从 Uri 中获取位图?

于 2013-02-16T14:49:25.197 回答
0

我想它会崩溃,因为这不是您从文件加载图像的方式。

该代码比您尝试的要简单得多:

Bitmap bmp = BitmapFactory.decodeFile(urienzo);

就这样!只要确保这条路径是正确的,因为它对我来说看起来不正确。

此外,如果您正在加载一个大图像(例如 4MP),它会因内存不足而崩溃,因为位图的想法是用来将当前大约高清到全高清分辨率的内容放在屏幕上。

于 2013-02-16T15:34:23.920 回答
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);

在哪里

private String getRealPathFromURI(Context context, Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
CursorLoader loader = new CursorLoader(context, contentUri, proj, null, null, null);
Cursor cursor = loader.loadInBackground();
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
于 2013-02-16T14:51:42.827 回答