我想从内置Android图库中检索ACTION_PICK
Intent中的照片。
我对Picasa的图片有疑问。
我已将代码用于此link,但它不起作用(File对象不存在)。
请问。
答案 0 :(得分:5)
ACTIVITYRESULT_CHOOSEPICTURE是调用startActivity时使用的int(intent,requestCode);
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();
}
}
答案 1 :(得分:0)
如果插入此指令,代码将起作用:
intent.putExtra("crop", "true");
答案 2 :(得分:0)
ACTION_GET_CONTENT
意图而不是ACTION
_ PICK MediaStore.EXTRA_OUTPUT
额外的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;
}
答案 3 :(得分:0)
使用此代码
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);
}