如何从android中的sdcard获取图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11740065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:40:03 来源:igfitidea点击:
How to get image from sdcard in android?
提问by Manish
i want create simple app in which i want get the image from sdcard but can`t success. my code is below
我想创建一个简单的应用程序,我想在其中从 sdcard 获取图像但无法成功。我的代码在下面
File myFile = new File("/sdcard/photo.jpg");
ImageView jpgView = (ImageView)findViewById(R.id.imageView);
BitmapDrawable d = new BitmapDrawable(getResources(), myFile.getAbsolutePath());
jpgView.setImageDrawable(d);
but can`t get the image.
但无法获得图像。
回答by Dipak Keshariya
Use below code instead of your code.
使用下面的代码而不是您的代码。
File f = new File("/mnt/sdcard/photo.jpg");
ImageView mImgView1 = (ImageView)findViewById(R.id.imageView);
Bitmap bmp = BitmapFactory.decodeFile(f.getAbsolutePath());
mImgView1.setImageBitmap(bmp);
回答by user2185718
package com.example.facebook;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class Facebookhome extends Activity {
Button share;
Uri screenshotUri;
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_facebookhome);
share = (Button) findViewById(R.id.button1);
img = (ImageView) findViewById(R.id.imageView1);
share.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(intent, "Select Picture"),
SELECT_PICTURE);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
img.setImageURI(selectedImageUri);
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
}
回答by MAC
try this...
尝试这个...
Bitmap bitmap = BitmapFactory.decodeFile(myFile.getAbsolutePath());
jpgView.setImageBitmap(bitmap);
回答by Abhishek Lokare
I guess there will definitely help you are is much simpler approach
我想肯定会帮助你是更简单的方法
Java Version:
爪哇版:
import static android.app.Activity.RESULT_OK;
private Bitmap bitmap;
private Uri filePath;
private void showFileChooser() {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, GET_IMAGE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GET_IMAGE && resultCode == RESULT_OK && data != null && data.getData() != null) {
filePath = data.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), filePath);
imageView1.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Kotlin Version:
科特林版本:
import android.app.Activity.RESULT_OK
private val GET_IMAGE = 1
private lateinit var filePath : Uri
private lateinit var bitmap : Bitmap
private fun selectImage() {
var photoPickerIntent : Intent = Intent(Intent.ACTION_PICK)
photoPickerIntent.setType("image/*")
startActivityForResult(photoPickerIntent, GET_IMAGE)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if(requestCode == GET_IMAGE && resultCode == RESULT_OK && data != null){
filePath = data.data
try{
bitmap = MediaStore.Images.Media.getBitmap(activity!!.contentResolver, filePath)
imageView!!.setImageBitmap(bitmap)
}catch (exception : IOException){
Toast.makeText(activity,"Error Loading Image!!!", Toast.LENGTH_SHORT).show()
}
}
}