user1024882
user1024882

Reputation: 43

How to save a jpg image to my own directory in sd card

I have updated the following code to following line changes but did not work:

//set the Image location

File file = new File(Environment.getExternalStorageDirectory() + "/Skynet/images/t1.jpg" );

Uri uriTarget = Uri.fromFile(file);

I want to save the jpeg to the above directory, but do not know since it is using the media store. Any tip to do this.

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class AndroCamera extends Activity {
   private static final int IMAGE_CAPTURE = 0;
   private Button startBtn;
   private Uri imageUri;
   private ImageView imageView;

/** Called when the activity is first created.
 *  sets the content and gets the references to
 *  the basic widgets on the screen like
 *  {@code Button} or {@link ImageView}
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    imageView = (ImageView)findViewById(R.id.img);
    startBtn = (Button) findViewById(R.id.startBtn);
    startBtn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            startCamera();
        }
    });
}

public void startCamera() {
    Log.d("ANDRO_CAMERA", "Starting camera on the phone...");
    String fileName = "testphoto.jpg";
    ContentValues values = new ContentValues();
    values.put(MediaStore.Images.Media.TITLE, fileName);
    values.put(MediaStore.Images.Media.DESCRIPTION,
            "Image capture by camera");
    values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
    imageUri = getContentResolver().insert(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
    startActivityForResult(intent, IMAGE_CAPTURE);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == IMAGE_CAPTURE) {
        if (resultCode == RESULT_OK){
            Log.d("ANDRO_CAMERA","Picture taken!!!");
            imageView.setImageURI(imageUri);
        }
    }
}

}

Upvotes: 1

Views: 3385

Answers (1)

Raykud
Raykud

Reputation: 2484

thats because the Uri you are using is from the media manager, perhaps if you use the defined Uri you want it to be saved to, it should work. Here is a hint:

mImageUri= Uri.fromFile( new File( Environment.getExternalStorageDirectory(),
                        "pic_" + String.valueOf( System.currentTimeMillis() ) + ".jpg" ) );

in this it was saved to the root, but since you are creating the file, then you can place it wherever you want to. Just make sure the directory exist otherwise create it. And as @Simon said, make sure you have the permission to write on external storage.

Update 1: currently you have something like:

imageUri = getContentResolver().insert(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

so the insert is only inserting the image to the MediaStore table... but if thats what you actually need, then you need to override the Data column in MediaStore. adding on your contentValues something like this:

values.put( MediaStore.Images.ImageColumns.DATA, fullPath );

if you do not need to use the MediaStore table then there is no need to do the insert and therefore the ContentValues aren't needed.

Upvotes: 4

Related Questions