我想從圖庫中創建圖片選擇器。我使用代碼android從圖庫中挑選圖片
intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, TFRequestCodes.GALLERY);
我的問題是,在這個活動和視頻文件顯示。有沒有辦法過濾顯示的文件,以便在此活動中不顯示視頻文件?
我想從圖庫中創建圖片選擇器。我使用代碼android從圖庫中挑選圖片
intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, TFRequestCodes.GALLERY);
我的問題是,在這個活動和視頻文件顯示。有沒有辦法過濾顯示的文件,以便在此活動中不顯示視頻文件?
絕對。試試這個:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
也不要忘記創建不斷PICK_IMAGE,這樣你就可以識別,當用戶從圖片庫活動回來:
public static final int PICK_IMAGE = 1;
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == PICK_IMAGE) {
//TODO: action
}
}
這就是我如何調用圖片庫。把它放進去,看看它是否適合你。
編輯:
這將帶來文檔應用程序。爲了讓用戶使用起來也有可能被安裝任何應用程序畫廊:
Intent getIntent = new Intent(Intent.ACTION_GET_CONTENT);
getIntent.setType("image/*");
Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickIntent.setType("image/*");
Intent chooserIntent = Intent.createChooser(getIntent, "Select Image");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {pickIntent});
startActivityForResult(chooserIntent, PICK_IMAGE);
有時候,你不能從你選擇的圖片文件。 這是因爲選擇的來自Google+,Drive,Dropbox或任何其他提供商。
最好的解決方案是讓系統通過Intent.ACTION_GET_CONTENT選擇一個內容,並獲得一個內容提供者的結果。
您可以按照下面的代碼或看看我的updated gist。
public void pickImage() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, PICK_PHOTO_FOR_AVATAR);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_PHOTO_FOR_AVATAR && resultCode == Activity.RESULT_OK) {
if (data == null) {
//Display an error
return;
}
InputStream inputStream = context.getContentResolver().openInputStream(data.getData());
//Now you can do whatever you want with your inpustream, save it as file, upload to a server, decode a bitmap...
}
}
if(resultCode == Activity.RESULT_OK){...}'可以使用檢測成功/取消 – 2014-02-04 13:57:03
如何獲取路徑? – delive 2015-10-09 10:27:07
@delive,我認爲你可以嘗試'新的File(data.getData())。getAbsolutePath()'只是一個猜測,我沒有嘗試過 – 2015-12-17 15:14:12
如果你只是在尋找圖片和多項選擇。
看@一旦https://stackoverflow.com/a/15029515/1136023
這有助於future.I個人感覺好極了使用MultipleImagePick。
將MultipleImagePick用作庫的最快捷方式是什麼?它來作爲一個獨立的項目/應用程序.. – ticofab 2014-09-06 11:44:32
public void FromCamera() {
Log.i("camera", "startCameraActivity()");
File file = new File(path);
Uri outputFileUri = Uri.fromFile(file);
Intent intent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, 1);
}
public void FromCard() {
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, 2);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 2 && 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]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
bitmap = BitmapFactory.decodeFile(picturePath);
image.setImageBitmap(bitmap);
if (bitmap != null) {
ImageView rotate = (ImageView) findViewById(R.id.rotate);
}
} else {
Log.i("SonaSys", "resultCode: " + resultCode);
switch (resultCode) {
case 0:
Log.i("SonaSys", "User cancelled");
break;
case -1:
onPhotoTaken();
break;
}
}
}
protected void onPhotoTaken() {
// Log message
Log.i("SonaSys", "onPhotoTaken");
taken = true;
imgCapFlag = true;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
bitmap = BitmapFactory.decodeFile(path, options);
image.setImageBitmap(bitmap);
}
謝謝,但ID不適用於API 25?! – 2017-06-12 10:32:46
非常感謝。這個答案幫助我稍後在 – 2017-08-21 01:25:25
上傳圖像我想你的意思是情況1:而不是情況-1:在OnActivityResult中進行切換。 – Mathias 2017-11-26 09:21:43
U可以比這做的答案很容易:
Uri Selected_Image_Uri = data.getData();
ImageView imageView = (ImageView) findViewById(R.id.loadedimg);
imageView.setImageURI(Selected_Image_Uri);
我這樣做,它的工作。如果你需要使用文件例如發送到服務器你必須使用其他方式,但只是爲了加載圖像到ImageView你可以做這個簡單的解決方案。 – 2015-10-29 20:52:12
歡迎來到SO!您也可以修改自己的帖子,而不是對自己的帖子發表評論。如果你的評論應該解釋答案,那麼爲什麼不把它放在答案本身呢?就我個人而言,我看不出你對這個老問題的回答(最近)與這個問題有關。我可以建議專注於首先回答問題,但尚未得到公認的答案嗎? – cfi 2015-10-29 21:23:15
我找不到android問題。我怎麼才能看到只是Android的問題? – 2015-10-31 01:53:45
您可以使用此方法從圖庫中選擇圖片。只會顯示圖像。
public void pickImage() {
Intent intent = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.INTERNAL_CONTENT_URI);
intent.setType("image/*");
intent.putExtra("crop", "true");
intent.putExtra("scale", true);
intent.putExtra("outputX", 256);
intent.putExtra("outputY", 256);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("return-data", true);
startActivityForResult(intent, 1);
}
,並覆蓋onActivityResult作爲
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK) {
return;
}
if (requestCode == 1) {
final Bundle extras = data.getExtras();
if (extras != null) {
//Get image
Bitmap newProfilePic = extras.getParcelable("data");
}
}
}
本文很好地介紹瞭如何選擇從畫廊圖像:http://androidbitmaps.blogspot.com/2015/04/loading-images-in-android- part-iii-pick.html – 2015-04-17 05:13:03
還有一個和你類似的問題。 http://stackoverflow.com/a/31382240/1835650 – TeeTracker 2015-07-13 11:39:45