Android intent-从图库中选择图像
我遵循这一点,以发送一个意图,以获得一个图像Android intent-从图库中选择图像,android,android-intent,Android,Android Intent,我遵循这一点,以发送一个意图,以获得一个图像 Intent i = new Intent(); i.setAction(Intent.ACTION_GET_CONTENT); i.setType("image/*"); if (i.resolveActivity(getPackageManager()) != null) { startActivityForResult(i, 3); } 在onActivityResult中: 发送意图后,图像选择器启动,我在库中选择一个图像。但
Intent i = new Intent();
i.setAction(Intent.ACTION_GET_CONTENT);
i.setType("image/*");
if (i.resolveActivity(getPackageManager()) != null) {
startActivityForResult(i, 3);
}
在onActivityResult中:
发送意图后,图像选择器启动,我在库中选择一个图像。但在onActivityResult中,对应于示例代码中的以下语句,我在Android 4.4中更改为getParcelableExtra:
Bitmap thumbnail = data.getParcelable("data");
我得到的缩略图为空。我正在测试Genymotion虚拟机
我做错了什么?你能试试他的答案吗?
使用getData而不是getParcelableExtra
它将返回一个Uri,您需要使用ContentResolver来获取图像
另外,用Intent.ACTION\u PICK代替Intent.ACTION\u GET\u CONTENT。你能试试他的答案吗?
使用getData而不是getParcelableExtra
它将返回一个Uri,您需要使用ContentResolver来获取图像
另外,请使用Intent.ACTION\u PICK而不是Intent.ACTION\u GET\u CONTENT。从数据中获取URI并自己加载位图,而不是获取位图
Uri selectedImageUri = data.getData();
String selectedImagePath = getPath(selectedImageUri);
不要获取位图,而是从数据中获取URI并自己加载位图
Uri selectedImageUri = data.getData();
String selectedImagePath = getPath(selectedImageUri);
试试这个。。
这对你有帮助
/**
* Images uploaded through gallery
*
*/
btn_gallery.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
if (v.getId() == R.id.btn_gallery)
{
upload = true;
gallery_upload();
}
}
});
/**
* By this method we can upload the image from gallery
*/
public void gallery_upload()
{
Log.d("Upload", "inside gallery upload");
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (upload)
{
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data)
{
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
picturePath = cursor.getString(columnIndex);
Log.e("", "" + picturePath);
cursor.close();
imageView = (ImageView) findViewById(R.id.upload_register_image);
File image = new File(picturePath);
if (image.exists())
{
File f = image.getAbsoluteFile();
decodeFile(f);
}
else
{
Log.d("VEHICLEREGISTERATION", "image doesnt exist");
}
upload = false;
imagePath(picturePath);
}
}
}
/**
* Helps to decode size of the image
* */
private Bitmap decodeFile(File f)
{
try
{
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 70;
// Find the correct scale value. It should be the power of 2.
int scale = 1;
while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
scale *= 2;
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
Bitmap bit_map = BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
imageView.setImageBitmap(bit_map);
}
catch (FileNotFoundException e)
{
}
return null;
}
public void imagePath(String Path)
{
finalImgPath = Path;
}
试试这个。。
这对你有帮助
/**
* Images uploaded through gallery
*
*/
btn_gallery.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
if (v.getId() == R.id.btn_gallery)
{
upload = true;
gallery_upload();
}
}
});
/**
* By this method we can upload the image from gallery
*/
public void gallery_upload()
{
Log.d("Upload", "inside gallery upload");
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (upload)
{
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data)
{
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
picturePath = cursor.getString(columnIndex);
Log.e("", "" + picturePath);
cursor.close();
imageView = (ImageView) findViewById(R.id.upload_register_image);
File image = new File(picturePath);
if (image.exists())
{
File f = image.getAbsoluteFile();
decodeFile(f);
}
else
{
Log.d("VEHICLEREGISTERATION", "image doesnt exist");
}
upload = false;
imagePath(picturePath);
}
}
}
/**
* Helps to decode size of the image
* */
private Bitmap decodeFile(File f)
{
try
{
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 70;
// Find the correct scale value. It should be the power of 2.
int scale = 1;
while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
scale *= 2;
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
Bitmap bit_map = BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
imageView.setImageBitmap(bit_map);
}
catch (FileNotFoundException e)
{
}
return null;
}
public void imagePath(String Path)
{
finalImgPath = Path;
}