【问题标题】:Creating a bitmap from png file stored at sdcard (Android)从存储在 sdcard (Android) 中的 png 文件创建位图
【发布时间】:2012-07-09 19:59:21
【问题描述】:

我试图从存储在 SD 卡上的 Png 文件创建位图,然后在 imageView 中设置该位图。 但它不起作用。

代码如下:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;

import com.pxr.tutorial.xmltest.R;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.widget.ImageView;

public class Getbackground {
    URL url;

    public static long downloadFile(URL url2) {
    try {

        URL url = new URL ("http://oranjelan.nl/oranjelan-bg.png");
        InputStream input = url.openStream();{
        try {

            File fileOnSD=Environment.getExternalStorageDirectory();    
            String storagePath = fileOnSD.getAbsolutePath();
            OutputStream output = new FileOutputStream (storagePath + "/oranjelanbg.png");
                try {

                    byte[] buffer = new byte[1024];
                    int bytesRead = 0;
                    while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
                    output.write(buffer, 0, bytesRead);
                    }
                    } finally {
            output.flush();
            output.close();
//----------------------------------------------------------------------------------------------------------
            Bitmap BckGrnd = BitmapFactory.decodeFile(storagePath + "/oranjelanbg.png");
            ImageView BackGround=(ImageView)findViewById(R.id.imageView1);
            BackGround.setImageBitmap(BckGrnd);
//----------------------------------------------------------=-----------------------------------------------
            }
            } catch (IOException e) {
            throw new RuntimeException(e);
    } finally {
        try {
            input.close();
            } catch (IOException e) {
            throw new RuntimeException(e);
     }
   }    
 }    
        } catch (MalformedURLException ex) {
         throw new RuntimeException(ex);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }

        return 0;
    }
//-----------------------------------------------------------------------------------------
    private static ImageView findViewById(int imageview1) {
        // TODO Auto-generated method stub
        return null;
    }
//-----------------------------------------------------------------------------------------
}

文件确实成功加载到 SD 卡上,但我似乎无法在视图中获取 img。

【问题讨论】:

  • hmm 似乎没有解决问题。

标签: android bitmap png


【解决方案1】:

你不能……

ImageView BackGround=(ImageView)findViewById(R.id.imageView1);
BackGround.setImageBitmap(BckGrnd);

如何在非活动类Getbackground 中获得ImageView 的引用?

您只能在 MainUI 线程中更新 UI 组件,如果它在非 Activity 类中,则只能使用该调用 Activity 类的引用(上下文)。

因此,请在 Getbackground 完成后将此代码放入您的 Activity 类中。

【讨论】:

  • 欢迎好友..!快乐编码..!
  • @user370305 你说 你如何在非活动类 Getbackground 中获取 ImageView 的引用? 但是如果我们将上下文和要更新的图像视图引用传递给 @987654324 @构造函数,那么就有可能。我对吗 ?对于图像的延迟加载,我们通常这样做。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-28
  • 1970-01-01
  • 1970-01-01
  • 2014-07-28
  • 1970-01-01
相关资源
最近更新 更多