Image laden und komprimieren

  • Antworten:1
Christian Auner
  • Forum-Beiträge: 18

13.07.2015, 19:03:23 via Website

Hallo Leute,

in meiner App lade ich Bilder aus meiner Gallery und speicher die Uri in einer Datenbank, um sie später wieder aufzurufen und die Bilder an anderer Stelle erneut darzustellen.

Folgendes Problem tritt jetzt auf:

W/OpenGLRenderer﹕ Bitmap too large to be uploaded into a texture (4160x3120, max=4096x4096)

Gibt es eine möglichkeit die Bilder zu komprimieren beim Laden und wann muss ich das dann machen?
Oder gibt es die Möglichkeit die Bilder in die App komprimiert zu laden und dann weiterzuverwenden.

Hier mein bestender Quellcode dazu:

 uebImageMainUri.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(intent, 0);
        }
    });

}

public void onActivityResult(int reqCode, int resCode, Intent data){
    if(resCode == RESULT_OK){
        if(resCode == Activity.RESULT_OK && data != null){
            String realPath="";
            String wholeID = DocumentsContract.getDocumentId(data.getData());

            // Split at colon, use second item in the array
            String id = wholeID.split(":")[1];
            String[] column = { MediaStore.Images.Media.DATA };

            // where id is equal to
            String sel = MediaStore.Images.Media._ID + "=?";

            Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                    column, sel, new String[]{ id }, null);

            int columnIndex = cursor.getColumnIndex(column[0]);

            if (cursor.moveToFirst()) {
                realPath = cursor.getString(columnIndex);
            }
            cursor.close();

            imageMain = Uri.fromFile(new File(realPath));
            uebImageMainUri.setImageURI(imageMain);
        }
    }
}

Wenn ich die Bilder wieder aufrufe sieht das so aus:

uebImageMainUri = (ImageView) findViewById(R.id.ivMainUeb);
    uebImageMainUri.setImageURI(dbHelper.uebList.get(uebPosition).get_imageMainUri());

Ich hoffe ihr habt eine Idee für mich.

Grüße

Antworten
Gelöschter Account
  • Forum-Beiträge: 2.640

19.07.2015, 02:15:34 via App

Google hat das in seinen Dokumentationen gut beschrieben:
http://developer.android.com/intl/ru/training/displaying-bitmaps/load-bitmap.html#load-bitmap

public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {

    final int halfHeight = height / 2;
    final int halfWidth = width / 2;

    // Calculate the largest inSampleSize value that is a power of 2 and keeps both
    // height and width larger than the requested height and width.
    while ((halfHeight / inSampleSize) > reqHeight
            && (halfWidth / inSampleSize) > reqWidth) {
        inSampleSize *= 2;
    }
}

return inSampleSize;

}
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {

// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);

// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);

}

Für weitere Fragen entweder im Link recherchieren oder einfach hier fragen

Ich sammle gerne Namen in meinem Death Note.

Samsung Galaxy Note 2<3 -> Samsung Galaxy Note 3 <3

Antworten