在android中创建可从sd卡提取的设置为背景
我正在尝试使用sd卡上的图像,并将其设置为relativelayout的背景。我已经尝试过在这里和其他地方找到的其他解决方案,但它们似乎对我不起作用。这是我的密码。我已经指出了我尝试过但不起作用的其他方法。唯一对我有效的方法是使用SetBackgroundResource和应用程序中的资源,但这只是为了测试以确保mRoot设置正确。当我尝试了所有其他的方法,它只是没有设置任何东西。有人知道我做错了什么,或者有没有更好的方法在android中创建可从sd卡提取的设置为背景,android,image,layout,background,sd-card,Android,Image,Layout,Background,Sd Card,我正在尝试使用sd卡上的图像,并将其设置为relativelayout的背景。我已经尝试过在这里和其他地方找到的其他解决方案,但它们似乎对我不起作用。这是我的密码。我已经指出了我尝试过但不起作用的其他方法。唯一对我有效的方法是使用SetBackgroundResource和应用程序中的资源,但这只是为了测试以确保mRoot设置正确。当我尝试了所有其他的方法,它只是没有设置任何东西。有人知道我做错了什么,或者有没有更好的方法 //one way i tired... //Strin
//one way i tired...
//String extDir = Environment.getExternalStorageDirectory().toString();
//Drawable d = Drawable.createFromPath(extDir + "/pic.png");
//mRoot.setBackgroundDrawable(d);
//another way tried..
//Drawable d = Drawable.createFromPath("/sdcard/pic.png");
//mRoot.setBackgroundDrawable(d);
//last way i tried...
mRoot.setBackgroundDrawable(Drawable.createFromPath(new File(Environment.getExternalStorageDirectory(), "pic.png").getAbsolutePath()));
//worked, only to verify mRoot was setup correctly and it could be changed
//mRoot.setBackgroundResource(R.drawable.bkg);
我建议检查牵引装置是否正确加载。有些事情可以尝试:
- 尝试在sd卡上使用其他图像
- 将pic.png放在R.drawable中,并确保mRoot.setBackgroundResource()达到预期效果
- 加载drawable后,请检查d.getBounds()以确保它符合预期
// Read bitmap from Uri
public Bitmap readBitmap(Uri selectedImage) {
Bitmap bm = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2; //reduce quality
AssetFileDescriptor fileDescriptor =null;
try {
fileDescriptor = this.getContentResolver().openAssetFileDescriptor(selectedImage,"r");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
finally{
try {
bm = BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options);
fileDescriptor.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return bm;
}
此处的Uri可以由库选择器活动提供
然后可以将图像保存到应用程序资源中并加载到imageView中
private void saveBackground(Bitmap Background) {
String strBackgroundFilename = "background_custom.jpg";
try {
Background.compress(CompressFormat.JPEG, 80, openFileOutput(strBackgroundFilename, MODE_PRIVATE));
} catch (Exception e) {
Log.e(DEBUG_TAG, "Background compression and save failed.", e);
}
Uri imageUriToSaveCameraImageTo = Uri.fromFile(new File(BackgroundSettings.this.getFilesDir(), strBackgroundFilename));
// Load this image
Bitmap bitmapImage = BitmapFactory.decodeFile(imageUriToSaveCameraImageTo.getPath());
Drawable bgrImage = new BitmapDrawable(bitmapImage);
//show it in a view
ImageView backgroundView = (ImageView) findViewById(R.id.BackgroundImageView);
backgroundView.setImageURI(null);
backgroundView.setImageDrawable(bgrImage);
}
谢谢你的回复。我尝试了你所说的,当图像作为一个资源出现在应用程序中时,它可以正常工作,但是当我尝试从SD卡中获取它(使用你建议的检查)时,我得到一个空指针异常。我假设它不会使绘图变得可绘制,但我不知道为什么createFromPath()在文件找不到或无法识别时不会抛出任何异常。我会仔细检查文件是否存在“adb shell ls-l/sdcard/pic.png”。其他人有什么建议吗?我是用位图做的,就像我在对另一个答案的评论中解释的那样,但它还不起作用,我得到的所有边界都是0。有什么想法吗?我想找不到文件了。用“adb shell ls-l/sdcard/pic.png”再次检查它是否在您认为的位置,感谢您的回复。因此我尝试了自己的方法,但仍然不起作用。因此,我使用图库选择器从图像中获取uri,然后调用您的函数并尝试将返回的位图设置为可绘制位图,但它不起作用。当我用getbounds检查它时,它为left、right、top和bottom返回0。知道我做错了什么吗?下面是我刚才描述的代码:uriselectedimage=imageReturnedIntent.getData();位图测试图像=读取位图(选择图像);BitmapDrawable bmd2=新的BitmapDrawable(Multimedia.this.getResources(),testimage);Rect t2=bmd2.getBounds();testimage在内存中,尚未保存在应用程序的资源中,您可能需要这样做才能保留它。因此,要获得一个可绘制文件,应该这样做:bmd2=新的位图可绘制文件(testimage);首先尝试使用小图像。正确的方法是将此位图传递给一个方法,该方法将在将其转换为可绘制位图之前将其缩放为背景并保存。在每个阶段,使用日志或Toast检查您得到了什么。在readBitmap int h=bitmap.getHeight()之后尝试此操作;int w=bitmap.getWidth();并显示h和w以查看大小。这个方法很有效,我在很多设备上都试过了…好的,所以我检查了位图的高度和宽度,得到了值。我该如何将其保存到应用程序资源中,还是bitmapdrawable可以做到这一点,而它只需要更多的时间来处理?我已经编辑了我的文章。我认为你现在应该有所有的代码来抓取一个图像,保存它并在应用程序中使用它。这将返回一个比存储在磁盘上更小的图像。知道为什么吗?
File file = new File( url.getAbsolutePath(), imageUrl);
if (file.exists()) {
mDrawable = Drawable.createFromPath(file.getAbsolutePath());
}