多くの人が以前にこの質問をし、それを解決する正しい方法を得ました. しかし、どういうわけか私にはうまくいきません。表示する大きな画像を選択しようとすると、アプリがクラッシュします。BitmapFactory を使用してサイズを縮小することも試みました。これは機能していません。助けてください。
私のコード:
public void browse(View v){
@SuppressWarnings("unused")
int id = v.getId();
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
InputStream stream = null;
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) {
Uri imgUri = data.getData();
logopath = getPath(imgUri);
//This block is to show the view
try {
if(bitmap != null){
bitmap.recycle();
}
stream = getContentResolver().openInputStream(data.getData());
bitmap = BitmapFactory.decodeStream(stream);
iv.setImageBitmap(Bitmap.createScaledBitmap(bitmap, 100, 100, true));
//bitmap = shrinkBitmap(logopath,100,100);
//iv.setImageBitmap(bitmap);
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
finally{
if (stream != null) {
try {
stream.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
private Bitmap shrinkBitmap(String file, int height, int width) {
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)height);
int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)width);
if (heightRatio > 1 || widthRatio > 1)
{
if (heightRatio > widthRatio)
{
bmpFactoryOptions.inSampleSize = heightRatio;
} else {
bmpFactoryOptions.inSampleSize = widthRatio;
}
}
bmpFactoryOptions.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
return bitmap;
}
LogCat の詳細:
06-25 07:16:26.056: E/dalvikvm-heap(1973): Out of memory on a 48000016-byte allocation.
06-25 07:16:26.206: E/AndroidRuntime(1973): FATAL EXCEPTION: main
06-25 07:16:26.206: E/AndroidRuntime(1973): java.lang.OutOfMemoryError
06-25 07:16:26.206: E/AndroidRuntime(1973): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
06-25 07:16:26.206: E/AndroidRuntime(1973): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:529)
06-25 07:16:26.206: E/AndroidRuntime(1973): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:601)
06-25 07:16:26.206: E/AndroidRuntime(1973): at com.example.tg_db1.edit_company.onActivityResult(edit_company.java:113)
06-25 07:16:26.206: E/AndroidRuntime(1973): at android.app.Activity.dispatchActivityResult(Activity.java:5293)
06-25 07:16:26.206: E/AndroidRuntime(1973): at android.app.ActivityThread.deliverResults(ActivityThread.java:3315)
06-25 07:16:26.206: E/AndroidRuntime(1973): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3362)
06-25 07:16:26.206: E/AndroidRuntime(1973): at android.app.ActivityThread.access$1100(ActivityThread.java:141)
06-25 07:16:26.206: E/AndroidRuntime(1973): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
06-25 07:16:26.206: E/AndroidRuntime(1973): at android.os.Handler.dispatchMessage(Handler.java:99)
06-25 07:16:26.206: E/AndroidRuntime(1973): at android.os.Looper.loop(Looper.java:137)
06-25 07:16:26.206: E/AndroidRuntime(1973): at android.app.ActivityThread.main(ActivityThread.java:5039)
06-25 07:16:26.206: E/AndroidRuntime(1973): at java.lang.reflect.Method.invokeNative(Native Method)
06-25 07:16:26.206: E/AndroidRuntime(1973): at java.lang.reflect.Method.invoke(Method.java:511)
06-25 07:16:26.206: E/AndroidRuntime(1973): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-25 07:16:26.206: E/AndroidRuntime(1973): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-25 07:16:26.206: E/AndroidRuntime(1973): at dalvik.system.NativeStart.main(Native Method)