Reputation: 167
I don't know why but my taken image doesn't display on my imageview. If i try to display an image that was already in my app ( in the mipmap folder for example ) it will display that, but it won't display an image I took. Is there something wrong with my code?
private File imageFile;
private static final int PHOTO_TAKEN = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photo_taker);
addSnapButtonListener();
}
private void addSnapButtonListener(){
Button snapBtn = (Button) findViewById(R.id.camera_button);
snapBtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
File picturesDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
imageFile = new File(picturesDirectory, "passpoints_image");
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile));
startActivityForResult(i, PHOTO_TAKEN);
}
});
}
protected void onActivityResult(int reqC, int resC, Intent data) {
if(reqC == PHOTO_TAKEN){
// Bitmap photo = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
Bitmap photo = (Bitmap) data.getExtras().get(imageFile.getAbsolutePath());
// Bitmap photo = (Bitmap) data.getExtras().get("data");
if(photo != null){
ImageView imageView = (ImageView)findViewById(R.id.taken_image);
imageView.setImageBitmap(photo);
} else {
Toast.makeText(this, R.string.unable_to_set_photo_file, Toast.LENGTH_LONG).show();
}
}
}
I tried a couple of different methods to get my taken image ( as you can see with the commented out code ) but whenever I take the picture and I look at the imageview it is still blank.
This also happens when I try to display an image from the gallery. Is it an issue with my phone?
Edit: also, the imagepath does get registered because I can retrieve it and display it in a toast or the log, so I don't believe that that's part of the issue.
Upvotes: 0
Views: 109
Reputation: 1007534
Bitmap photo = (Bitmap) data.getExtras().get(imageFile.getAbsolutePath());
That will return null
, as there will be no extra with that name.
This code, that you have commented out, is closer:
Bitmap photo = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
But, you need to retain imageFile
, as your activity may be destroyed or your process may be terminated while the camera app is in the foreground. And, Uri.fromFile()
will not work on Android 7.0+ devices, once you move your targetSdkVersion
to 24 or higher. Plus, do not call decode...()
on BitmapFactory
on the main application thread, as your UI will freeze while that disk I/O and computation is going on.
This sample app (from my book, FWIW), demonstrates using FileProvider
instead of Uri.fromFile()
and retaining the path using onSaveInstanceState()
. In my case, I show the image via ACTION_VIEW
instead of loading into an ImageView
:
/***
Copyright (c) 2008-2016 CommonsWare, LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License.
From _The Busy Coder's Guide to Android Development_
https://commonsware.com/Android
*/
package com.commonsware.android.camcon;
import android.app.Activity;
import android.content.ClipData;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import java.io.File;
import java.util.List;
public class CameraContentDemoActivity extends Activity {
private static final String EXTRA_FILENAME=
"com.commonsware.android.camcon.EXTRA_FILENAME";
private static final String FILENAME="CameraContentDemo.jpeg";
private static final int CONTENT_REQUEST=1337;
private static final String AUTHORITY=
BuildConfig.APPLICATION_ID+".provider";
private static final String PHOTOS="photos";
private File output=null;
private Uri outputUri=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (savedInstanceState==null) {
output=new File(new File(getFilesDir(), PHOTOS), FILENAME);
if (output.exists()) {
output.delete();
}
else {
output.getParentFile().mkdirs();
}
}
else {
output=(File)savedInstanceState.getSerializable(EXTRA_FILENAME);
}
outputUri=FileProvider.getUriForFile(this, AUTHORITY, output);
if (savedInstanceState==null) {
i.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP) {
i.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
else if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN) {
ClipData clip=
ClipData.newUri(getContentResolver(), "A photo", outputUri);
i.setClipData(clip);
i.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
else {
List<ResolveInfo> resInfoList=
getPackageManager()
.queryIntentActivities(i, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
grantUriPermission(packageName, outputUri,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
}
startActivityForResult(i, CONTENT_REQUEST);
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable(EXTRA_FILENAME, output);
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == CONTENT_REQUEST) {
if (resultCode == RESULT_OK) {
Intent i=new Intent(Intent.ACTION_VIEW);
i.setDataAndType(outputUri, "image/jpeg");
i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(i);
finish();
}
}
}
}
If I were going to load this into an ImageView
, I would use an image-loading library like Picasso.
Upvotes: 1