creating a drawable from sd card to set as a background in android

前端 未结 3 646
北恋
北恋 2021-01-03 10:18

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 h

相关标签:
3条回答
  • 2021-01-03 10:36

    I suggest checking that the drawable is being loaded correctly. Some things to try:

    • Try using a different image on the sd card
    • Put pic.png in R.drawable and make sure mRoot.setBackgroundResource() does what you expect
    • After loading the drawable, check d.getBounds() to make sure it is what you expect
    0 讨论(0)
  • 2021-01-03 10:44
    File file = new File( url.getAbsolutePath(), imageUrl);
    
                    if (file.exists()) {
    
                        mDrawable = Drawable.createFromPath(file.getAbsolutePath());
    
                    }
    
    0 讨论(0)
  • 2021-01-03 10:52

    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);
        }
    
    0 讨论(0)
提交回复
热议问题