Reputation: 7377
This simple code ask users to pick an image from gallery
private void openGallery() {
Intent gallery =
new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(gallery, PICK_IMAGE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == PICK_IMAGE) {
Uri imageUri = data.getData();
imageView.setImageURI(imageUri);
}
}
}
My question : I see many codes or apps creates a folder when a user upload an image. Should I do that ? and how to create a folder with the above code
Upvotes: 0
Views: 56
Reputation: 501
Check below code for create folder dynamically.
String root = Environment.getExternalStorageDirectory()
.toString();
new File(root + "/" + Constants.IMAGE_DIRECTORY_NAME + "/"
+ Constants.SUB_DIRECTORY_NAME).mkdirs();
Please create bitmap file for your selected gallery image and save into your own created folder.
File outputfile = new File(root + "/"
+ Constants.IMAGE_DIRECTORY_NAME + "/"
+ Constants.SUB_DIRECTORY_NAME + "/", "img_"
+ dateFormatter.format(new Date()).toString() + ".jpeg");
OutputStream outStream = null;
try {
outStream = new FileOutputStream(outputfile);
bmFrame.compress(Bitmap.CompressFormat.JPEG, 70, outStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.e(TAG, e.toString());
} finally {
try {
if (outStream != null) {
outStream.flush();
outStream.close();
bmFrame.recycle();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
use above code in onActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == PICK_IMAGE) {
Uri imageUri = data.getData();
imageView.setImageURI(imageUri);
Bitmap bmFrame = MediaStore.Images.Media.getBitmap(
this.getContentResolver(), imageUri);
//Add above code here.
}
}
Upvotes: 2