android - 将图像从 sdcard 加载到 imageview 不工作

标签 android imageview sd-card

尝试了很多方法,但没有取得积极的成果。 在 TableLayout 中有 ImageView:

<TableLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/photo_layout_background"
    android:stretchColumns="*" >

    <TableRow
        android:background="@color/photo_layoutrow_background"
        android:layout_weight="1"
        android:layout_marginBottom="4dp"
        android:gravity="center" >

    <ImageView
        android:id="@+id/photo1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginRight="4dp"
        android:src="@drawable/icon_photo"
        android:background="@color/photo_background"
        android:clickable="true"
        android:onClick="addPhoto_Click"/>

当你点击ImageView提供获取图片的标准程序并且文件保存在SD卡上时:

public void addPhoto_Click(View v){
    select_add_photo_id = v.getId();
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, generateFilePhotoUri());
    startActivityForResult(takePictureIntent, CAMERA_RESULT);
}

@SuppressLint("SimpleDateFormat")
private Uri generateFilePhotoUri() {
    File file = null;
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    file = new File(MEDIA_PATH + String.valueOf(id) + File.separator + timeStamp + ".jpg");
    select_add_photo_file = file.getAbsolutePath();
    return Uri.fromFile(file);
}   

照片通常保存在变量 select_add_photo_id id ImageView 中,点击 select_add_photo_file 变量保存文件照片的完整路径。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_RESULT) {
        if (resultCode == RESULT_OK){
            ImageView imgView = (ImageView) findViewById(select_add_photo_id);
            imgView.setImageDrawable(Drawable.createFromPath(select_add_photo_file));

        }

    }
}   

但是当文件只获取带有背景和增大尺寸的ImageView ImageView 时。摄影未加载。没有错误。

作为静态照片?

最佳答案

接电话后,在onActivityResult中,data.getData可能为null,此时试试data.getExtras()

Bitmap photo = null;  
Uri photoUri = data.getData();  
String filePath = "";
if (photoUri != null) {  
    //mFilePath = getRealPathFromURI(photoUri);
    filePath = getRealPathFromURI(photoUri);
    photo = BitmapFactory.decodeFile(photoUri.getPath());  
}  
if (photo == null) {  
    Bundle extra = data.getExtras();  
    if (extra != null) {  
         photo = (Bitmap)extra.get("data");
...

关于android - 将图像从 sdcard 加载到 imageview 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21249776/

相关文章:

Android APIDemos 编译错误

java - 底部导航菜单点击图标选择

android - Selendroid 作为网络抓取工具

android - 如何从服务器保存图像并在以后重用

android - 即使没有抛出错误,将数据库复制到 SD 卡也会失败

android - 如何写入文本文件

java - 解码Android的反编译源代码

android - 底部边距或填充在 android 上的 xml 中的相对布局中不起作用

android - 列表绑定(bind)后在自定义 ListView 中设置 ImageView 的源

java - 是否可以将数据库文件保存到SD卡?