我想在已存储的SD卡中显示ImageView中的Bitmap图像.运行后我的应用程序崩溃并导致OutOfMemoryError错误:
(java.lang.OutOfMemoryError:无法分配带有2097152个空闲字节的23970828字节分配和2MB直到OOM)
我不知道或为什么它的内存不足.我认为我的图像尺寸非常大,所以我试图改变它.
Iterator<String> it = imageArray.iterator();
while (it.hasNext()) {
Object element = it.next();
String objElement = element.toString();
Log.e("objElement ", " = " + objElement);
final ImageView imageView = new ImageView (getContext());
final ProgressBar pBar = new ProgressBar(getContext(), null,
android.R.attr.progressBarStyleSmall);
imageView.setTag(it);
pBar.setTag(it);
imageView.setImageResource(R.drawable.img_placeholder);
pBar.setVisibility(View.VISIBLE);
if (objElement.endsWith(mp3_Pattern)) {
Log.e("Mp3 ", " ends with ");
pBar.setVisibility(View.GONE);
imageView.setImageResource(R.drawable.audio_control);
}
if (objElement.endsWith(png_Pattern)) {
Bitmap bitmap = BitmapFactory.decodeFile(objElement);
int size = Math.min(bitmap.getWidth(), bitmap.getHeight());
int x = (bitmap.getWidth() - size) …Run Code Online (Sandbox Code Playgroud) 我正试图在我的应用程序内打开Gallery内置应用程序中的图像/图片.
我有一张图片的URI(图片位于SD卡上).
你有什么建议吗?
我在一个文件中有一个大位图(比如3888x2592).现在,我想将该位图的大小调整为800x533并将其保存到另一个文件中.我通常会通过调用Bitmap.createBitmap方法来缩放位图,但它需要一个源位图作为第一个参数,我无法提供,因为将原始图像加载到Bitmap对象当然会超出内存(例如,请参见此处).
我也无法读取位图,例如,BitmapFactory.decodeFile(file, options)提供a BitmapFactory.Options.inSampleSize,因为我想将其调整为精确的宽度和高度.使用inSampleSize会将位图的大小调整为972x648(如果我使用inSampleSize=4)或778x518(如果我使用inSampleSize=5,它甚至不是2的幂).
我还想避免使用inSampleSize在第一步中使用例如972x648读取图像,然后在第二步中将其大小调整为800x533,因为与直接调整原始图像大小相比,质量会很差.
总结我的问题:是否有办法读取10MP或更高的大图像文件并将其保存到新的图像文件,调整大小到特定的新宽度和高度,而不会得到OutOfMemory异常?
我也尝试BitmapFactory.decodeFile(file, options)手动将Options.outHeight和Options.outWidth值设置为800和533,但它不起作用.
宣言android.graphics.Bitmap.createScaledBitmap是
public static Bitmap createScaledBitmap
(Bitmap src, int dstWidth, int dstHeight, boolean filter)
Run Code Online (Sandbox Code Playgroud)
但是,文档没有解释任何参数.所有这些都非常明显,除了boolean filter.有谁知道它的作用?
我有一个应用程序,我正在设置近50个类android:largeHeap="true",如下所示.这是一个好习惯吗?
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="Mall"
android:largeHeap="true"
android:logo="@drawable/logo_for_up"
android:screenOrientation="portrait"
android:theme="@style/AppTheme" >
</application>
Run Code Online (Sandbox Code Playgroud)
请建议使用它的优点和缺点.
我得到记忆问题,这就是我问这个问题的原因.
假设我在一个位图对象中加载了一个图像
Bitmap myBitmap = BitmapFactory.decodeFile(myFile);
Run Code Online (Sandbox Code Playgroud)
现在,如果我加载另一个位图,会发生什么
myBitmap = BitmapFactory.decodeFile(myFile2);
Run Code Online (Sandbox Code Playgroud)
第一个myBitmap会发生什么?它是否收集垃圾或者我必须在加载另一个位图之前手动垃圾收集它,例如. myBitmap.recycle()?
此外,是否有更好的方法来加载大图像并在途中回收时一个接一个地显示它们?
有没有人知道Android手机上的堆大小是否是根据操作系统版本中设置的常量值,或者这是电话制作者可以决定的设置?
堆大小是否与手机上的RAM量成正比?
我只找到了人们说应用程序的堆大小为16M的文章.但是,这些文章有点陈旧.从我看来,作为一个例子,在一个特定型号上,堆大小从大约20M到24M不等.这款手机拥有768M的RAM.
我对使用Java InputStreams时close()方法的用法有一些疑问.从我看到和从大多数开发人员那里读到的内容,你应该总是在不再需要的时候在InputStream上显式调用close().但是,今天我正在研究使用Java属性文件,我发现的每个例子都有这样的:
Properties props = new Properties();
try {
props.load(new FileInputStream("message.properties"));
//omitted.
} catch (Exception ex) {}
Run Code Online (Sandbox Code Playgroud)
使用上面的示例,无法显式调用close(),因为InputStream在使用后无法访问.我已经看到了许多类似的InputStreams用法,尽管它似乎与大多数人对明确关闭的说法相矛盾.我通读了Oracle的JavaDocs,并没有提到Properties.load()方法是否关闭了InputStream.我想知道这是否普遍可以接受,或者是否更喜欢做更像以下的事情:
Properties props = new Properties();
InputStream fis = new FileInputStream("message.properties");
try {
props.load(fis);
//omitted.
} catch (Exception ex) {
//omitted.
} finally {
try {
fis.close();
} catch (IOException ioex) {
//omitted.
}
}
Run Code Online (Sandbox Code Playgroud)
哪种方式更好和/或更有效?或者它真的重要吗?
尽管我在drawable文件夹中有一个非常小的图像,我从用户那里得到了这个错误.我没有在代码中使用任何位图功能.至少故意:)
java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:683)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:513)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:889)
at android.content.res.Resources.loadDrawable(Resources.java:3436)
at android.content.res.Resources.getDrawable(Resources.java:1909)
at android.view.View.setBackgroundResource(View.java:16251)
at com.autkusoytas.bilbakalim.SoruEkrani.cevapSecimi(SoruEkrani.java:666)
at com.autkusoytas.bilbakalim.SoruEkrani$9$1.run(SoruEkrani.java:862)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Run Code Online (Sandbox Code Playgroud)
根据这个stackTrace,我在这一行得到了这个错误('tv'是一个textView):
tv.setBackgroundResource(R.drawable.yanlis);
Run Code Online (Sandbox Code Playgroud)
问题是什么?如果您需要有关代码的其他信息,我可以添加它.谢谢!
我正在从Url下载图像并显示它们.在下载时它正在给予out of memory error : bitmap size exceeds VM budget.我正在使用drawable.代码如下:
HttpClient httpclient= new DefaultHttpClient();
HttpResponse response=(HttpResponse)httpclient.execute(httpRequest);
HttpEntity entity= response.getEntity();
BufferedHttpEntity bufHttpEntity=new BufferedHttpEntity(entity);
InputStream instream = bufHttpEntity.getContent();
Bitmap bm = BitmapFactory.decodeStream(instream);
Bitmap useThisBitmap = Bitmap.createScaledBitmap(bm,bm.getWidth(),bm.getHeight(), true);
bm.recycle();
BitmapDrawable bt= new BitmapDrawable(useThisBitmap);
System.gc();
Run Code Online (Sandbox Code Playgroud)
这是错误: 05-28 14:55:47.251: ERROR/AndroidRuntime(4188): java.lang.OutOfMemoryError: bitmap size exceeds VM budget