user1461473
user1461473

Reputation: 41

Save image view as a jpeg image in sdcard

public class MapDemoActivity extends Activity {

Button capture;

    ImageView image;
    int cameracode=100;
    Bitmap bm;
    Boolean result;
    FileOutputStream fos;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        capture=(Button)findViewById(R.id.capture);
        capture.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                image=(ImageView)findViewById(R.id.image);
                Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(i, cameracode);
                image.setDrawingCacheEnabled(true);
                bm = image.getDrawingCache();

                try {
                    fos = new FileOutputStream("sdcard/image.jpg");
                    result=bm.compress(CompressFormat.JPEG, 75, fos);

                    fos.flush();
                    fos.close();
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block

                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block

                    e.printStackTrace();
                }


            }
        });

    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub

                    if(requestCode==100)
                    {

                        bm=(Bitmap) data.getExtras().get("data");
                        image.setImageBitmap(bm);


                    }

        super.onActivityResult(requestCode, resultCode, data);
    }
}

In this prog i am capturing image from camera & display to Image view then i am trying to convert it to Jpeg to store in sdcard... But when i pressed capture image Button prog get force close.. & empty jpeg file created in sdcard... i want to store jpeg file to sdcard

Upvotes: 0

Views: 3208

Answers (3)

Stephane Mathis
Stephane Mathis

Reputation: 6622

I think that the part of code after startActivityForResult is executed, and the bitmap is not yet available. You should save it directly on the onActivityResult.

Futhermore, have you ask Android to save you image by giving the intent this :

final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile(this)) );   
startActivityForResult(intent, TAKE_PHOTO_CODE);  

getTempFile is a function which returns me the path I want.

PS: Have you ask for the permission in the manifest ? You need

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA" />

Upvotes: 0

Aleks G
Aleks G

Reputation: 57336

startActivityForResult is an asynchronous call, therefore everything that you have right after that call is executed immediately, before waiting for the capture to complete. Instead of having it that way, you should move the saving code into the onActivityResult.

Additionally, you should not hardcode sdcard but instead use Environment.getExternalStorageDirectory() instead.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    capture=(Button)findViewById(R.id.capture);
    capture.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            image=(ImageView)findViewById(R.id.image);
            Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(i, cameracode);
        }
    });

}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub

    if(requestCode==100)
    {

        bm=(Bitmap) data.getExtras().get("data");
        image.setImageBitmap(bm);

        if(bm == null) {
            return; //probably user cancelled;
        }

        try {
            fos = new FileOutputStream(new File(Environment.getExternalStorageDirectory(), "image.jpg"));
            result=bm.compress(CompressFormat.JPEG, 75, fos);

            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block

            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block

            e.printStackTrace();
        }

    }
    super.onActivityResult(requestCode, resultCode, data);
}

Finally, you may want to check that you actually got an image before trying to save it, as the user may have cancelled image capture.

Apart from it, I suggest that you post your full logcat stack trace. Maybe there's something else sinister in there.

Upvotes: 1

Jack Satriano
Jack Satriano

Reputation: 2029

Try changing your target directory to this:

try {
                fos = new FileOutputStream("/mnt/sdcard/MyActivity/image.jpg");
                result=bm.compress(CompressFormat.JPEG, 75, fos);

                fos.flush();
                fos.close();

Make sure you create your directory before writing to it as well.

            String dir  = "/mnt/sdcard/MyActivity/";
        directory = new File(dir);
        if (!directory.exists()) {
            directory.mkdirs();
        }

Upvotes: 0

Related Questions