2017-02-22 143 views
0

我知道关于此主题还有其他几个问题,但没有一个解决方案适用于我。我在清单中添加了权限。我可以打开画廊选择一张照片并返回到应用程序,但imageView不会更改。该应用正在做一些处理,我没有得到任何错误。我试图在将图像插入到imageview之前尝试缩放图像,但没有运气。从图片库上传的图片不会显示在ImageView中

这里是我的代码:

public class UserDetailsFragment extends Fragment { 
private Context context; 
private ImageView imageView; 
private Bitmap uploadedImage; 
public static final int RESULT_IMAGE = 0; 


public UserDetailsFragment() { 
    // Required empty public constructor 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    context = getActivity(); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    return inflater.inflate(R.layout.fragment_user_details, container, false); 
} 

@Override 
public void onViewCreated(View view, Bundle savedInstanceState){ 
    imageView = (ImageView) getView().findViewById(R.id.profile_picture); 
    imageView.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
      startActivityForResult(i, RESULT_IMAGE); 
     } 
    }); 
} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if(resultCode == RESULT_OK){ 
     Uri selectedImage = data.getData(); 
     String[] filePathColumn = {MediaStore.Images.Media.DATA}; 
     Cursor cursor = context.getContentResolver().query(
       selectedImage, filePathColumn, null, null, null); 
     cursor.moveToFirst(); 
     int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
     String filePath = cursor.getString(columnIndex); 
     cursor.close(); 
     try { 
      InputStream in = new URL(filePath).openStream(); 
      uploadedImage = BitmapFactory.decodeStream(in); 
      imageView.setImageBitmap(uploadedImage); 
     }catch (Exception ex){ 

     } 
    } 
} 

}

这里是我的ImageView:

<ImageView 
    android:layout_width="100dp" 
    android:layout_height="100dp" 
    android:layout_marginTop="-160dp" 
    android:layout_gravity="center" 
    android:layout_below="@+id/imageView" 
    android:src="@drawable/user_details_icon" 
    android:id="@+id/profile_picture" /> 

编辑

我试图用毕加索代替,但仍然没有运气:

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if(resultCode == RESULT_OK){ 


     Picasso.with(context) 
       .load(MediaStore.Images.Media.DATA) 
       .resize(10, 10) 
       .centerCrop() 
       .into(imageView); 

    } 
+2

首先,**不要在没有记录它的情况下捕获一个'Exception',因为你是'onActivityResult()'。其次,您的'onActivityResult()'代码是错误的,因为您可能无权处理文件路径(例如,它指向可移动存储),并且您正在主应用程序线程上执行磁盘I/O。使用[图像加载库](https://android-arsenal.com/tag/46),像[Picasso](https://github.com/square/picasso),将'Uri'传递给正确使用'Uri'并且异步填充'ImageView'。 – CommonsWare

+0

感谢您的提示。我对Android相当陌生,所以我实际上并不了解毕加索。我不确定我明白你的意思onActivityResult(),但我试图改变它到内部存储,而不是仍然没有运气。我也改变了我的整个onActivityResult(见编辑),但仍然没有运气 – Andypandy

回答

0

我试图用毕加索代替,但仍然没有运气:

那是因为你没有使用正确的Uri。您正在使用指向扫描媒体的整个集合的Uri

如果你读the documentation for ACTION_PICK,它具有:

输出:这是选对项目的URI。

这里,有问题的Uri可以通过调用传递到onActivityResult()IntentgetData()获得。因此,用.load(data.getData())代替。

此外,最有可能的是,您需要增加图像的大小,因为10像素x 10像素将会相当小。

+0

现在我得到 E/MiniThumbFile:无法创建.thumbnails目录/storage/emulated/0/DCIM/.thumbnails – Andypandy

+0

@Andypandy:请确保你有'WRITE_EXTERNAL_STORAGE'作为权限,包括处理运行时权限,如果你的'targetSdkVersion'是23或更高。 – CommonsWare

+0

我已经添加了在我的清单中读取和写入外部存储的权限。我的目标sdk是19 – Andypandy

1

在你前面的代码,问题出在下面几行:

InputStream in = new URL(filePath).openStream(); 
uploadedImage = BitmapFactory.decodeStream(in); 

这些行创建例外,因为这是负责从服务器下载图像,并将其转换位图。当你从本地存储采摘图像,图像的路径是一样的东西下面

/storage/emulated/0/DCIM/Camera/IMG_20170209_183830841.jpg

当你通过它新URL()作为参数,它创建以下异常

java.net。MalformedURLException的:未发现协议

在你的情况首先你需要在你的清单文件,并在onActivityResult()添加READ_EXTERNAL_STORAGE许可的,你有

更换

InputStream in = new URL(filePath).openStream(); 
uploadedImage = BitmapFactory.decodeStream(in); 

File image = new File(filePath); 
BitmapFactory.Options bmOptions = new BitmapFactory.Options(); 
Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(), bmOptions); 
bitmap = Bitmap.createScaledBitmap(bitmap, imageView.getWidth(), imageView.getHeight(), true); 

注:如果是棉花糖,请不要忘记添加安全许可你的代码。