android uri.getpath,image gallery - android get real path by Uri.getPath() - Stack Overflow

Hii here is my complete code for taking image from camera or galeery

//My variable declaration

protected static final int CAMERA_REQUEST = 0;

protected static final int GALLERY_REQUEST = 1;

Bitmap bitmap;

Uri uri;

Intent picIntent = null;

//Onclick

if (v.getId()==R.id.image_id){

startDilog();

}

//method body

private void startDilog() {

AlertDialog.Builder myAlertDilog = new AlertDialog.Builder(yourActivity.this);

myAlertDilog.setTitle("Upload picture option..");

myAlertDilog.setMessage("Where to upload picture????");

myAlertDilog.setPositiveButton("Gallery", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

picIntent = new Intent(Intent.ACTION_GET_CONTENT,null);

picIntent.setType("image/*");

picIntent.putExtra("return_data",true);

startActivityForResult(picIntent,GALLERY_REQUEST);

}

});

myAlertDilog.setNegativeButton("Camera", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

picIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

startActivityForResult(picIntent,CAMERA_REQUEST);

}

});

myAlertDilog.show();

}

//And rest of things

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

if (requestCode==GALLERY_REQUEST){

if (resultCode==RESULT_OK){

if (data!=null) {

uri = data.getData();

BitmapFactory.Options options = new BitmapFactory.Options();

options.inJustDecodeBounds = true;

try {

BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options);

options.inSampleSize = calculateInSampleSize(options, 100, 100);

options.inJustDecodeBounds = false;

Bitmap image = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options);

imageofpic.setImageBitmap(image);

} catch (FileNotFoundException e) {

e.printStackTrace();

}

}else {

Toast.makeText(getApplicationContext(), "Cancelled",

Toast.LENGTH_SHORT).show();

}

}else if (resultCode == RESULT_CANCELED) {

Toast.makeText(getApplicationContext(), "Cancelled",

Toast.LENGTH_SHORT).show();

}

}else if (requestCode == CAMERA_REQUEST) {

if (resultCode == RESULT_OK) {

if (data.hasExtra("data")) {

bitmap = (Bitmap) data.getExtras().get("data");

uri = getImageUri(YourActivity.this,bitmap);

File finalFile = new File(getRealPathFromUri(uri));

imageofpic.setImageBitmap(bitmap);

} else if (data.getExtras() == null) {

Toast.makeText(getApplicationContext(),

"No extras to retrieve!", Toast.LENGTH_SHORT)

.show();

BitmapDrawable thumbnail = new BitmapDrawable(

getResources(), data.getData().getPath());

pet_pic.setImageDrawable(thumbnail);

}

} else if (resultCode == RESULT_CANCELED) {

Toast.makeText(getApplicationContext(), "Cancelled",

Toast.LENGTH_SHORT).show();

}

}

}

private String getRealPathFromUri(Uri tempUri) {

Cursor cursor = null;

try {

String[] proj = { MediaStore.Images.Media.DATA };

cursor = this.getContentResolver().query(tempUri, proj, null, null, null);

int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

cursor.moveToFirst();

return cursor.getString(column_index);

} finally {

if (cursor != null) {

cursor.close();

}

}

}

public static int calculateInSampleSize(

BitmapFactory.Options options, int reqWidth, int reqHeight) {

// Raw height and width of image

final int height = options.outHeight;

final int width = options.outWidth;

int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {

final int halfHeight = height / 2;

final int halfWidth = width / 2;

// Calculate the largest inSampleSize value that is a power of 2 and keeps both

// height and width larger than the requested height and width.

while ((halfHeight / inSampleSize) > reqHeight

&& (halfWidth / inSampleSize) > reqWidth) {

inSampleSize *= 2;

}

}

return inSampleSize;

}

private Uri getImageUri(YourActivity youractivity, Bitmap bitmap) {

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);

String path = MediaStore.Images.Media.insertImage(youractivity.getContentResolver(), bitmap, "Title", null);

return Uri.parse(path);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值