Reputation: 139
I'm aware that there are several other questions on this topic but none of the solutions worked for me. I have added permissions in my manifest. I am able to open the gallery pick a photo and return to the application but the imageView does not change. The app is doing some processing and I am not getting any errors. I have tried also tried to scale the image before inserting it into my imageview with no luck.
Here is my code:
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){
}
}
}
}
Here is my 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" />
EDIT
I tried to use Picasso instead but still no luck:
@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);
}
Upvotes: 0
Views: 275
Reputation: 704
In Your previous code, the problem is in the following lines:
InputStream in = new URL(filePath).openStream();
uploadedImage = BitmapFactory.decodeStream(in);
these lines are creating exception because that is responsible for downloading image from server and covert it into Bitmap. As you are picking image from local storage, the path of image is something like following
/storage/emulated/0/DCIM/Camera/IMG_20170209_183830841.jpg
when you pass it in new URL() as a parameter, it creates following exception
java.net.MalformedURLException: Protocol not found
In your case first of all you need to add READ_EXTERNAL_STORAGE permission in your manifest file and in onActivityResult(), you have to replace
InputStream in = new URL(filePath).openStream();
uploadedImage = BitmapFactory.decodeStream(in);
with
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);
Note: In case of marshmallow, don't forget to add security permission in your code.
Upvotes: 1
Reputation: 1007099
I tried to use Picasso instead but still no luck:
That is because you are not using the correct Uri
. You are using a Uri
that points to the entire collection of scanned media.
If you read the documentation for ACTION_PICK
, it has:
Output: The URI of the item that was picked.
Here, the Uri
in question is obtained by calling getData()
on the Intent
delivered to onActivityResult()
.
So, replace .load(MediaStore.Images.Media.DATA)
with .load(data.getData())
.
Also, most likely, you need to increase the size of the image, as a 10px x 10px image is going to be rather small.
Upvotes: 0