在我的应用程序中,我使用intent
从库中获取图像文件名
我试图将此文件名加载到TImage
。我尝试使用LoadFromFile
加载JBitmap
并将其转换为TBitmap
并将Assign
转换为TImage
。这仅适用于最大尺寸为98 x 98像素的位图...如果宽度或高度大于99或98像素大部分时间我收到错误消息'位图大小太大'。有时我可以达到250 x 250 ......
IM试图找到为什么从3天开始......
以下是代码:
var
jBitmapD, jSmallBitmap: JBitmap;
BitmapD : TBitmap;
imgW, imgH:Integer;
begin
...
jBitmapD := TJBitmapFactory.JavaClass.decodeFile(StringToJString(imgGallerieChm));
...
jSmallBitmap := TJBitmap.JavaClass.createScaledBitmap(jBitmapD, 98, 98, True);
//jSmallBitmap := TJBitmap.JavaClass.createScaledBitmap(jBitmapD, 101, 101, True);
//jSmallBitmap := TJBitmap.JavaClass.createScaledBitmap(jBitmapD, 1234, 567, True);
...
imgH := jSmallBitmap.getHeight;
imgW := jSmallBitmap.getWidth;
bitmapD := TBitmap.Create; //Work if jSmallBitmap if smaller than 99 x 99
//bitmapD.Create(98,98); //Work if jSmallBitmap if smaller than 99 x 99
//bitmapD.Create(222,333); //Don't work
try
bitmapD.SetSize(imgW,ImgH); //without this, its not working and it should be 99 x 99 or less... most of the time
bitmapD := JBitmapToBitmap(jSmallBitmap);
imgLepObs.Bitmap.Assign(bitmapD);
finally
bitmapD.Free;
end;
如果AImage和TBitmap大部分时间小于99 x 99,则下一个功能
function TfrmMain.JBitmapToBitmap(const AImage: JBitmap): TBitmap;
var
bitmapSurface :TBitmapSurface;
begin
bitmapSurface := TBitmapSurface.Create;
try
if JBitmapToSurface(AImage, bitmapSurface) then
begin
Result.Assign(bitmapSurface);
end;
finally
bitmapSurface.Free;
end;
end;
感谢您的帮助......即将放弃...
我尝试使用此缩减我的位图:
bfOptions.inSampleSize := StrToInt(Edit1.Text); //
bfOptions.inJustDecodeBounds := False;
jSmallBitmap := TJBitmapFactory.JavaClass.decodeFile(StringToJString(imgGallerieChm), bfOptions);
它大部分时间都在工作......结果位图越小,成功的可能性就越大......对于给定的大小,它有时会工作,有时不工作,有时它会在我重新启动应用程序后立即工作。
答案 0 :(得分:2)
我遇到了类似的问题。问题是delphi RTL for android中的已知错误。如果您在一个线程中创建一个图像,您将获得一个例外:TBitmap.Create引发的位图大小太大'。 这是因为帆布工厂无法获得正确的canvastype。它必须由EMB确定。我在主线程中创建我的位图,现在可以使用。
答案 1 :(得分:0)
我建议的解决方法是解码较大的图像。
BitmapFactory类提供了几种解码方法(decodeByteArray(),decodeFile(),decodeResource()等。)。
请参阅以下SO链接以获取我的答案。我提供了解码图像的步骤和代码示例。
OutOfMemoryError with image selection in Android. How to resize image before decoding it to bitmap?
答案 2 :(得分:-2)
public class ImageLoader {
MemoryCache memoryCache=new MemoryCache();
FileCache fileCache;
private Map<ImageView, String> imageViews=Collections.synchronizedMap(new WeakHashMap<ImageView, String>());
ExecutorService executorService;
public ImageLoader(Context context){
fileCache=new FileCache(context);
executorService=Executors.newFixedThreadPool(5);
}
final int stub_id=R.drawable.ic_launcher;
public void DisplayImage(String url, ImageView imageView)
{
imageViews.put(imageView, url);
Bitmap bitmap=memoryCache.get(url);
if(bitmap!=null)
imageView.setImageBitmap(bitmap);
else
{
queuePhoto(url, imageView);
imageView.setImageResource(stub_id);
}
}
private void queuePhoto(String url, ImageView imageView)
{
PhotoToLoad p=new PhotoToLoad(url, imageView);
executorService.submit(new PhotosLoader(p));
}
private Bitmap getBitmap(String url)
{
File f=fileCache.getFile(url);
//from SD cache
Bitmap b = decodeFile(f);
if(b!=null)
return b;
//from web
try {
Bitmap bitmap=null;
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(true);
InputStream is=conn.getInputStream();
OutputStream os = new FileOutputStream(f);
Utils.CopyStream(is, os);
os.close();
bitmap = decodeFile(f);
return bitmap;
} catch (Throwable ex){
ex.printStackTrace();
if(ex instanceof OutOfMemoryError)
memoryCache.clear();
return null;
}
}
//decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f){
try {
//decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);
//Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE=70;
int width_tmp=o.outWidth, height_tmp=o.outHeight;
int scale=1;
while(true){
if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
break;
width_tmp/=2;
height_tmp/=2;
scale*=2;
}
//decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
return null;
}
//Task for the queue
private class PhotoToLoad
{
public String url;
public ImageView imageView;
public PhotoToLoad(String u, ImageView i){
url=u;
imageView=i;
}
}
class PhotosLoader implements Runnable {
PhotoToLoad photoToLoad;
PhotosLoader(PhotoToLoad photoToLoad){
this.photoToLoad=photoToLoad;
}
@Override
public void run() {
if(imageViewReused(photoToLoad))
return;
Bitmap bmp=getBitmap(photoToLoad.url);
memoryCache.put(photoToLoad.url, bmp);
if(imageViewReused(photoToLoad))
return;
BitmapDisplayer bd=new BitmapDisplayer(bmp, photoToLoad);
Activity a=(Activity)photoToLoad.imageView.getContext();
a.runOnUiThread(bd);
}
}
boolean imageViewReused(PhotoToLoad photoToLoad){
String tag=imageViews.get(photoToLoad.imageView);
if(tag==null || !tag.equals(photoToLoad.url))
return true;
return false;
}
//Used to display bitmap in the UI thread
class BitmapDisplayer implements Runnable
{
Bitmap bitmap;
PhotoToLoad photoToLoad;
public BitmapDisplayer(Bitmap b, PhotoToLoad p){bitmap=b;photoToLoad=p;}
public void run()
{
if(imageViewReused(photoToLoad))
return;
if(bitmap!=null)
photoToLoad.imageView.setImageBitmap(bitmap);
else
photoToLoad.imageView.setImageResource(stub_id);
}
}
public void clearCache() {
memoryCache.clear();
fileCache.clear();
}
}