Reputation: 89
I want to create a folder to store the captured image. From this, the image is taken however, the file directory is not created, and once I tilt the device, the image will be permanently gone. How should I proceed on? I have looked for many examples online and thus derived with such solution. I'm new to android platform but I'm learning. Any help is appreciated! Thanks =)
public class MainActivity extends Activity {
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.imageView = (ImageView)this.findViewById(R.id.imageView1);
Button photoButton = (Button) this.findViewById(R.id.button1);
photoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == CAMERA_REQUEST && resultCode == RESULT_OK){
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
File directory = new File(Environment.getExternalStorageDirectory()+File.separator+"imageView");
if (!directory.exists()) {
directory.mkdirs();
}
}
}
static public boolean hasStorage(boolean requireWriteAccess) {
//TODO: After fix the bug, add "if (VERBOSE)" before logging errors.
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
if (requireWriteAccess) {
boolean writable = checkFsWritable();
return writable;
} else {
return true;
}
} else if (!requireWriteAccess && Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return true;
}
return false;
}
private static boolean checkFsWritable() {
// TODO Auto-generated method stub
return false;
}
}
Upvotes: 2
Views: 220
Reputation: 5234
use below code
photoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
String filepath = Environment.getExternalStorageDirectory()
.getPath();
File file = new File(filepath, "/AudioRecorder/" + image.jpg);
if (!file.exists()) {
file.mkdirs();
}
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(file ));
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
and add this to your code after onCreate
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Intent i = new Intent(this, Activity2.class);
startActivity(i);
}
}
Upvotes: 2
Reputation: 6201
If you still need to access the SD card, the Camera application that checks if the SD card is mounted as follows:
static public boolean hasStorage(boolean requireWriteAccess) {
//TODO: After fix the bug, add "if (VERBOSE)" before logging errors.
String state = Environment.getExternalStorageState();
Log.v(TAG, "storage state is " + state);
if (Environment.MEDIA_MOUNTED.equals(state)) {
if (requireWriteAccess) {
boolean writable = checkFsWritable();
Log.v(TAG, "storage writable is " + writable);
return writable;
} else {
return true;
}
} else if (!requireWriteAccess && Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return true;
}
r
eturn false;
}
Upvotes: 3