下载的图像用作我的框架布局中的背景图像
我有一个下载的文件,我想在我的框架布局中用作背景图像。我使用这段代码,但它给了我一个错误。
这是我收到错误的行:
fm.setBackgroundDrawable(dr);
这是我的整个代码
public class MyAppActivity extends Activity {
private final String PATH = "/data/data/com.myapp/"; //put the downloaded file here
private Drawable imgsrc;
private FrameLayout fm;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FrameLayout fm = (FrameLayout)findViewById(R.id.framelyt);
//Drawable drw = ImageOperations(this,url,filename)
Button btn = (Button)findViewById(R.id.postbutton);
btn.setOnClickListener(test);
}
View.OnClickListener test = new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
DownloadFromUrl("http://www.mydomain.com/radio/images/", "image.png");
}
};
private void DownloadFromUrl(String imageURL, String fileName) {
// TODO Auto-generated method stub
try {
URL url = new URL(imageURL + fileName); //you can write here any link
File file = new File(PATH + fileName);
if(file.exists())
{
file.delete();
}
long startTime = System.currentTimeMillis();
Log.d("ImageManager", "download begining");
Log.d("ImageManager", "download url:" + url);
Log.d("ImageManager", "downloaded file name:" + fileName);
/* Open a connection to that URL. */
URLConnection ucon = url.openConnection();
/*
* Define InputStreams to read from the URLConnection.
*/
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
/*
* Read bytes to the Buffer until there is nothing more to read(-1).
*/
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
/* Convert the Bytes read to a String. */
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
Bitmap bMap = BitmapFactory.decodeFile(PATH + fileName);
BitmapDrawable dr = new BitmapDrawable(bMap);
// Drawable d = new BitmapDrawable();
fm.setBackgroundDrawable(dr);
fos.close();
Log.d("ImageManager", "download ready in "
+ ((System.currentTimeMillis() - startTime) / 1000)
+ " sec");
//fm.setBackgroundDrawable(d);
} catch (IOException e) {
Log.d("ImageManager", "Error: " + e);
}
}
}
I have a downloaded file and i want to use as background image in my framelayout. I use this code but it gives me an error .
here is the line where i get the error:
fm.setBackgroundDrawable(dr);
and here is my whole code
public class MyAppActivity extends Activity {
private final String PATH = "/data/data/com.myapp/"; //put the downloaded file here
private Drawable imgsrc;
private FrameLayout fm;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FrameLayout fm = (FrameLayout)findViewById(R.id.framelyt);
//Drawable drw = ImageOperations(this,url,filename)
Button btn = (Button)findViewById(R.id.postbutton);
btn.setOnClickListener(test);
}
View.OnClickListener test = new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
DownloadFromUrl("http://www.mydomain.com/radio/images/", "image.png");
}
};
private void DownloadFromUrl(String imageURL, String fileName) {
// TODO Auto-generated method stub
try {
URL url = new URL(imageURL + fileName); //you can write here any link
File file = new File(PATH + fileName);
if(file.exists())
{
file.delete();
}
long startTime = System.currentTimeMillis();
Log.d("ImageManager", "download begining");
Log.d("ImageManager", "download url:" + url);
Log.d("ImageManager", "downloaded file name:" + fileName);
/* Open a connection to that URL. */
URLConnection ucon = url.openConnection();
/*
* Define InputStreams to read from the URLConnection.
*/
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
/*
* Read bytes to the Buffer until there is nothing more to read(-1).
*/
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
/* Convert the Bytes read to a String. */
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
Bitmap bMap = BitmapFactory.decodeFile(PATH + fileName);
BitmapDrawable dr = new BitmapDrawable(bMap);
// Drawable d = new BitmapDrawable();
fm.setBackgroundDrawable(dr);
fos.close();
Log.d("ImageManager", "download ready in "
+ ((System.currentTimeMillis() - startTime) / 1000)
+ " sec");
//fm.setBackgroundDrawable(d);
} catch (IOException e) {
Log.d("ImageManager", "Error: " + e);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论