我不知道为什么,但我的拍摄图像并没有显示在我的imageview上。如果我尝试显示已经在我的应用程序中的图像(例如在mipmap文件夹中),它将显示该图像,但它不会显示我拍摄的图像。我的代码有问题吗?
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();
}
}
}
我尝试了几种不同的方法来获取我拍摄的图像(正如您可以看到注释掉的代码)但是每当我拍摄照片并且我看到它仍然是空白时。
当我尝试从图库中显示图像时,也会发生这种情况。这是我手机的问题吗?
编辑:此外,imagepath确实已注册,因为我可以检索它并将其显示在烤面包或日志中,因此我不相信这个问题的一部分。
答案 0 :(得分:1)
Bitmap photo = (Bitmap) data.getExtras().get(imageFile.getAbsolutePath());
这将返回null
,因为没有额外的名称。
您已注释掉的此代码更接近:
Bitmap photo = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
但是,您需要保留imageFile
,因为您的活动可能会被销毁,或者您的流程可能会在相机应用处于前台时终止。一旦将Uri.fromFile()
移至24或更高,targetSdkVersion
将无法在Android 7.0+设备上运行。另外,不要在主应用程序线程上的decode...()
上调用BitmapFactory
,因为当磁盘I / O和计算正在进行时,UI将冻结。
This sample app(来自my book,FWIW)演示了使用FileProvider
代替Uri.fromFile()
并使用onSaveInstanceState()
保留路径。在我的情况下,我会通过ACTION_VIEW
显示图片,而不是加载到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();
}
}
}
}
如果我要将其加载到ImageView
,我会使用an image-loading library之类的Picasso。