创建从存储在SD卡PNG文件中的位图(安卓) [英] Creating a bitmap from png file stored at sdcard (Android)

查看:156
本文介绍了创建从存储在SD卡PNG文件中的位图(安卓)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着去创建存储在SD卡上的PNG文件位图,然后设置位图在ImageView的。 但它不工作。

这里的code:

 进口的java.io.File;
进口java.io.FileOutputStream中;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.io.OutputStream中;
进口java.net.MalformedURLException;
进口的java.net.URL;

进口com.pxr.tutorial.xmltest.R;

进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;
进口android.os.Environment;
进口android.widget.ImageView;

公共类{的getBackground
    URL网址;

    公共静态长downloadFile(URL URL2){
    尝试 {

        网址URL =新的URL(http://oranjelan.nl/oranjelan-bg.png);
        输入的InputStream = url.openStream(){
        尝试 {

            文件fil​​eOnSD = Environment.getExternalStorageDirectory();
            串storagePath = fileOnSD.getAbsolutePath();
            的OutputStream输出=新的FileOutputStream(storagePath +/oranjelanbg.png);
                尝试 {

                    byte []的缓冲区=新的字节[1024];
                    INT读取动作= 0;
                    而((读取动作= input.read(缓冲液,0,buffer.length))> = 0){
                    output.write(缓冲液,0,读取动作);
                    }
                    } 最后 {
            output.flush();
            output.close();
//----------------------------------------------------------------------------------------------------------
            位图BckGrnd = BitmapFactory.de codeFILE(storagePath +/oranjelanbg.png);
            ImageView的背景=(ImageView的)findViewById(R.id.imageView1);
            BackGround.setImageBitmap(BckGrnd);
//----------------------------------------------------------=-----------------------------------------------
            }
            }赶上(IOException异常E){
            抛出新的RuntimeException(E);
    } 最后 {
        尝试 {
            input.close();
            }赶上(IOException异常E){
            抛出新的RuntimeException(E);
     }
   }
 }
        }赶上(MalformedURLException的前){
         抛出新的RuntimeException(前);
    }赶上(IOException异常E){
      抛出新的RuntimeException(E);
    }

        返回0;
    }
// ------------------------------------------------ -----------------------------------------
    私有静态ImageView的findViewById(INT imageview1){
        // TODO自动生成方法存根
        返回null;
    }
// ------------------------------------------------ -----------------------------------------
}
 

该文件并加载SD卡上的成功地,但我似乎无法得到视图中的IMG。

解决方案

您不能..

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

你怎么能得到的ImageView 在非活性类参考的getBackground

您只能在MainUI线程更新UI组件,如果在非活动类,那么使用调用活动类的引用(上下文)只。<​​/ P>

所以把这个code在你的Activity类完成的getBackground后。

Im trying to create a Bitmap from a Png file stored on the SD card and then set that Bitmap in an imageView. But its not working.

Here's the code:

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;
    }
//-----------------------------------------------------------------------------------------
}

The File does load on the SD card succesfully but I cant seem to get the img in the view.

解决方案

You can't..

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

How can you get the reference of ImageView in non activity class Getbackground?

You can only update UI component in MainUI Thread and if its in non Activity class then using reference (Context) of that calling activity class only.

So put this code in your Activity class after Getbackground completes.

这篇关于创建从存储在SD卡PNG文件中的位图(安卓)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆