Search code examples
javaandroidbitmapfileoutputstream

Copy existing png file and rename programmatically


I have a png file in a folder "Movies" on the sdcard. I want to copy and rename that file in the same folder. I'm confused on how to properly call the method SaveImage.

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
    if (scanningResult != null) {
        isbn = scanningResult.getContents();
        SaveImage();
    }
    else{
        Toast toast = Toast.makeText(getApplicationContext(),
                "No scan data received!", Toast.LENGTH_SHORT);
        toast.show();
    }
}


private void SaveImage(Bitmap finalBitmap){
    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/Movies/");
    String fname = "Image-"+ isbn +".jpg";
    File file = new File (myDir, fname);
    try {
        FileOutputStream out = new FileOutputStream(file);
        finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
        out.flush();
        out.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

Solution

  • So your question is, how to properly call your SaveImage(Bitmap finalBitmap) method, right ? as your SaveImage method get a Bitmap as parameter you need to send it a Bitmap as parameter.

    You can use BitmapFactory to create a Bitmap object from your file and send this Bitmap object to your SaveImage method :

    String root = Environment.getExternalStorageDirectory().toString();
    Bitmap bMap = BitmapFactory.decodeFile(root + "/Movies/myimage.png");
    SaveImage(bMap);