创建从SD卡的可绘制设置为在Android的背景 [英] creating a drawable from sd card to set as a background in android

查看:137
本文介绍了创建从SD卡的可绘制设置为在Android的背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用图像从SD卡,并设置为背景的RelativeLayout的。我已经试过,我已经在这里和其他地方发现了其他的解决方案,但他们还没有似乎为我工作。这里是我的code。我有评论指出,我已经尝试过其他方法,并没有工作。这为我工作的唯一的事情是使用setBackgroudnResource,并使用从应用程序的资源,但是这只是为了测试,以确保mRoot被设置正确。当我已经尝试了所有其他的方式,它只是没有设置任何东西。任何人都知道我在做什么错,或者是否有更好的方法来做到这一点?

  //一条路我累了...
//字符串extDir = Environment.getExternalStorageDirectory()的toString();
//绘制对象D = Drawable.createFromPath(extDir +/pic.png);
//mRoot.setBackgroundDrawable(d);

//另一种尝试..
//绘制对象D = Drawable.createFromPath(/ SD卡/ pic.png);
//mRoot.setBackgroundDrawable(d);

//最后一种方法我试过...
mRoot.setBackgroundDrawable(Drawable.createFromPath(新文件(Environment.getExternalStorageDirectory(),pic.png)getAbsolutePath()));

//工作,只是为了确认mRoot是正确的设置,它可以改变
//mRoot.setBackgroundResource(R.drawable.bkg);
 

解决方案

您不加载从SD卡,但一个位图绘制。这里有一个方法来与降低的采样(质量)加载它使程序不会抱怨,如果图像过大。然后,我猜你需要处理这个位图,即裁剪和调整的背景。

  //读取来自乌里位图
     公共位图readBitmap(URI selectedImage){
         位图BM = NULL;
         BitmapFactory.Options选项=新BitmapFactory.Options();
         options.inSampleSize = 2; //降低质量
         AssetFileDescriptor文件描述符= NULL;
         尝试 {
             文件描述符= this.getContentResolver()openAssetFileDescriptor(selectedImage,R)。
         }赶上(FileNotFoundException异常E){
             e.printStackTrace();
         }
         最后{
             尝试 {
                 BM = BitmapFactory.de codeFileDescriptor(fileDescriptor.getFileDescriptor(),空,期权);
                 fileDescriptor.close();
             }赶上(IOException异常E){
                 e.printStackTrace();
             }
         }
         返回BM;
     }
 

的URI这里可以从画廊选择器的活动提供。

然后可以保存到应用程序资源和装载到ImageView的图像

 私人无效saveBackground(位图背景){
        字符串strBackgroundFilename =background_custom.jpg;
        尝试 {
            Background.com preSS(比较pressFormat.JPEG,80,openFileOutput(strBackgroundFilename,MODE_PRIVATE));
        }赶上(例外五){
            Log.e(DEBUG_TAG,背景融为一体pression和保存失败。,E);
        }

        乌里imageUriToSaveCameraImageTo = Uri.fromFile(新文件(BackgroundSettings.this.getFilesDir(),strBackgroundFilename));

        //装载这个图片
        位图的BitmapImage = BitmapFactory.de codeFILE(imageUriToSaveCameraImageTo.getPath());
        可绘制bgrImage =新BitmapDrawable(的BitmapImage);

        //显示它在视图中
        ImageView的backgroundView =(ImageView的)findViewById(R.id.BackgroundImageView);
        backgroundView.setImageURI(空);
        backgroundView.setImageDrawable(bgrImage);
    }
 

I am trying to use an image from the sd card and set it as the background for a relativelayout. I have tried other solutions that i have found here and elsewhere but they havent seemed to work for me. here is my code. I have commented out other ways that i have tried and didnt work. the only thing that worked for me was using setBackgroudnResource and using a resource from the app, but this was just to test to make sure mRoot was set up correctly. when I have tried all the other ways, it just doesn't set anything. Anyone know what I am doing wrong, or if there is a better way to do this?

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

解决方案

You do not load a drawable from SD card but a bitmap. Here is a method to load it with the reduced sampling (quality) so the program will not complain if the image is too large. Then I guess you need to process this bitmap i.e. crop it and resize for the background.

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

The Uri here can be supplied from a gallery picker activity.

The image then can be saved into application resources and loaded into an 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卡的可绘制设置为在Android的背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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