2013-09-27 50 views
0

我必須將圖像保存到sqlite數據庫。它正在工作。例如,我可以保存來自相機的照片。但是當我從照片庫(700kb或更多)中選擇一張照片時,它不會保存到數據庫中。因此,我認爲我可以製作較小尺寸的照片。因此,我在下面編寫了這些代碼;如何縮小從圖庫中選擇的照片

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if(requestCode == SELECT_PHOTO && resultCode == RESULT_OK){ 
     Uri selectedImage = data.getData(); 
     InputStream imageStream = null; 
     try { 
      imageStream = getContentResolver().openInputStream(selectedImage); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 
     //Resmin boyutu küçültüldükten sonra DB'ye atılacak.102400,512000 
     Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream); 
     cekilenFoto.setImageBitmap(yourSelectedImage); 
     resimData = getBitmapAsByteArray(yourSelectedImage); 
    } 
} 

它並不小。它正在關閉設備上的應用程序。這裏出了什麼問題?

+0

post stacktrace。 – njzk2

回答

0

我認爲如此重新縮放圖像的作品,並減少圖像的大小。

if (yourSelectedImage!= null) { 
     Bitmap resizedBitmap = Bitmap.createScaledBitmap(bit, width, 
       height, true); 
    cekilenFoto.setImageBitmap(resizedBitmap); 
    resimData = getBitmapAsByteArray(resizedBitmap); 
    } 

寬和高和你可以在int中給出的可變大小。 ex。

int width=500; 
int height=500; 
0

雖然斯里達爾的答案會工作,有一點要記住的是,它需要首先解碼全尺寸InputStream中,增加了內存使用情況。如果InputStream表示一個非常大的圖像,這可能是一個問題,因爲它可能會導致OutOfMemoryExceptions。下面是將直接從的InputStream縮放位圖的方法:

public static Bitmap scaledBitmapFromStream(Context context, InputStream tempIs) { 
    // Buffer the InputStream so that it can be accessed twice. 
    InputStream is = new BufferedInputStream(tempIs); 

    // Find the dimensions of the input image, and calculate the sampleSize. 
    BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeStream(is, null, options); 
    options.inJustDecodeBounds = false; 

    // Calculate the inSampleSize. This is the factor that the image will be rescaled by. 
    options.inSampleSize = calculateInSampleSize(context, options.outWidth, options.outHeight); 

    // Reset the input stream, so it can be read again. 
    try { 
     is.reset(); 
    } catch (IOException e) { 
     throw new RuntimeException("BufferedInputStream.reset() failed.", e); 
    } 

    // The 'options' parameter here tells the BitmapFactory to downscale. 
    Bitmap output = BitmapFactory.decodeStream(is, null, options); 

    // Close the input stream. 
    try { 
     is.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    return output; 
} 

/** 
* How much you want to downsample will vary based on your application, but this implementation 
* calculates a safe display size based on devices screen resolution and OpenGL MAX_TEXTURE_SIZE. 
*/ 
public static int calculateInSampleSize(Context context, int inputWidth, int inputHeight) { 
    DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics(); 
    final int maxWidth = Math.min(displayMetrics.widthPixels, GLES20.GL_MAX_TEXTURE_SIZE); 
    final int maxHeight = Math.min(displayMetrics.heightPixels, GLES20.GL_MAX_TEXTURE_SIZE); 

    int inSampleSize = 1; 
    if (inputWidth > maxWidth || inputHeight > maxHeight) { 

     // Calculate ratios of height and width to requested height and width 
     final int heightRatio = Math.round((float) inputHeight/(float) maxHeight); 
     final int widthRatio = Math.round((float) inputWidth/(float) maxWidth); 

     // Choose the smallest ratio as inSampleSize value, this will guarantee 
     // a final image with both dimensions larger than or equal to the 
     // requested height and width. 
     inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; 
    } 

    return inSampleSize; 
} 

對inJustDecodeBounds和位圖採樣的更多信息,請參見https://developer.android.com/training/displaying-bitmaps/load-bitmap.html

+0

我用like,InputStream imageStream = null; \t \t \t嘗試{ \t \t \t \t的ImageStream = getContentResolver()openInputStream(selectedImage)。 \t \t \t}趕上(FileNotFoundException異常E){ \t \t \t \t e.printStackTrace(); \t \t \t} // Resmin boyutuküçültüldüktensonra DB'yeatılacak.102400,512000 \t \t \t位圖yourSelectedImage = scaledBitmapFromStream(getApplicationContext(),的ImageStream); – emreturka

+0

它不起作用 – emreturka

+0

java.lang.IllegalStateException:無法從CursorWindow讀取第0行,第0列。在從中訪問數據之前,確保Cursor已正確初始化。 – emreturka

相關問題