wiki
wiki

Reputation: 289

Decode file to bitmap

I have an activity that takes a picture saved in the SD and after the snapshot. I managed to take a picture and save it but then that saved him turn it into a bitmap file.

This is my class:

public class CamaraFicha extends Activity {

    private Button bt_hacerfoto;  
    private ImageView img; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camara_ficha);

         img = (ImageView)this.findViewById(R.id.imageView1);

         String rutaFoto = "/mnt/extSdCard/UGREP/";
         File ruta_sd = new File(rutaFoto);
         File f = new File(ruta_sd.getAbsolutePath(), "foto1.jpg");
         Uri uriSavedImage = Uri.fromFile(f);
         Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
         cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
         startActivityForResult(cameraIntent, 1);
    }


       protected void onActivityResult(int requestCode, int resultCode, Intent data) {  

             if (requestCode == 1 && resultCode == RESULT_OK) {  
                String rutaFoto = "/mnt/extSdCard/UGREP/";
                File ruta_sd = new File(rutaFoto);
                File f = new File(ruta_sd.getAbsolutePath(), "foto1.jpg");
                Bitmap bMap = BitmapFactory.decodeFile(f);
                img.setImageBitmap(bMap);        
             } 
       }
}

Can someone help me decode the file to turn it into a bitmap in the method protected void onActivityResult

Tranks for your help!!

Upvotes: 0

Views: 2631

Answers (2)

Jonas Czech
Jonas Czech

Reputation: 12358

In this case, BitmapFactory.decodeFile() needs the path to the file as a String, not as a File. So in your case, you need to do:

Bitmap bMap = BitmapFactory.decodeFile(f.getAbsolutePath());

Or just use a string with the path to your file directly, instead of creating a File first.

Upvotes: 1

Simas
Simas

Reputation: 44188

BitmapFactory.decodeFile() expects a String path. You can get it from your file like so:

BitmapFactory.decodeFile(f.getAbsolutePath());

Upvotes: 2

Related Questions