Create a card from an SD card to set as background in android

I am trying to use an image from an SD card and set it as the background for relativelayout. I tried other solutions that I found here and elsewhere, but they seemed to work for me. here is my code. I commented on other ways that I tried and did not work. the only thing that worked for me was to use setBackgroudnResource and use the resource from the application, but that was just to verify that mRoot was configured correctly. when I tried all other methods, it doesn’t install anything. Does anyone know what I'm 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);
+3
source share
3 answers

SD-, . (), , . , , , .

         // 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);
    }
+10
File file = new File( url.getAbsolutePath(), imageUrl);

                if (file.exists()) {

                    mDrawable = Drawable.createFromPath(file.getAbsolutePath());

                }
+5

drawable. , :

  • SD-.
  • pic.png R.drawable , mRoot.setBackgroundResource() ,
  • d.getBounds(), , ,
0

Source: https://habr.com/ru/post/1787189/


All Articles