【发布时间】:2012-01-09 22:47:05
【问题描述】:
我想从调用ACTION_PICK Intent 的内置Android 图库中检索照片。
我对 Picasa 的图片有疑问。
我已经使用了这个link 的代码,但它不起作用(文件对象不存在)。
有什么想法,请。
【问题讨论】:
标签: android android-intent android-gallery
我想从调用ACTION_PICK Intent 的内置Android 图库中检索照片。
我对 Picasa 的图片有疑问。
我已经使用了这个link 的代码,但它不起作用(文件对象不存在)。
有什么想法,请。
【问题讨论】:
标签: android android-intent android-gallery
ACTIVITYRESULT_CHOOSEPICTURE 是调用 startActivity(intent, requestCode); 时使用的 int;
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == ACTIVITYRESULT_CHOOSEPICTURE) {
BitmapFactory.Options options = new BitmapFactory.Options();
final InputStream is = context.getContentResolver().openInputStream(intent.getData());
final Bitmap bitmap = BitmapFactory.decodeStream(is, null, options);
is.close();
}
}
【讨论】:
如果插入这条指令,代码就可以工作:
intent.putExtra("crop", "true");
【讨论】:
ACTION_GET_CONTENT 意图而不是 ACTION_PICKMediaStore.EXTRA_OUTPUT extra 提供一个临时文件的 URI。 将此添加到您的通话活动中:
归档你的文件;
现在使用这个code to get Intent:
yourFile = getFileStreamPath("yourTempFile");
yourFile.getParentFile().mkdirs();
Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT, null);
galleryIntent .setType("image/*");
galleryIntent .putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(yourFile));
galleryIntent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.name());
startActivityForResult(galleryIntent, GALLERY_PIC_REQUEST);
确保 yourFile 已创建
也在您的通话活动中
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode){
case GALLERY_PIC_REQUEST:
File file = null;
Uri imageUri = data.getData();
if (imageUri == null || imageUri.toString().length() == 0) {
imageUri = Uri.fromFile(mTempFile);
file = mTempFile;
//this is the file you need! Check it
}
//if the file did not work we try alternative method
if (file == null) {
if (requestCode == 101 && data != null) {
Uri selectedImageUri = data.getData();
String selectedImagePath = getPath(selectedImageUri);
//check this string to extract picasa id
}
}
break;
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
if(cursor!=null)
{
int index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(index);
}
else return null;
}
【讨论】:
使用此代码
final Uri tempUri = data.getData();
Uri imageUri = null;
final InputStream imageStream;
try {
imageStream = getActivity().getContentResolver().openInputStream(tempUri);
Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
imageUri = getImageUri(getActivity(), selectedImage);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
【讨论】: