我正在使用相机,尝试拍摄,保存和显示图像。我的应用程序允许拍照,而Intent成功拍摄。我得到一个位图。此外,它保存文件,但我无法显示图像。
我通过使用FileProvider修复了权限问题,我知道文件正在保存,因为我的代码到达if(file.exists)块内部。在我模拟的Nexus 5X(API 25)上,我得到了#34;无法加载照片",而在我的物理Nexus 5X(API 27)上,我得到一个带有加载圈的黑屏,但没有图像负荷。
public class MainActivity extends Activity {
final static int MY_PERMISSIONS_REQUEST_CAMERA = 169;
private static final int REQUEST_IMAGE = 100;
protected static final String DEBUG_TAG = "MainActivity";
TextView tvPath;
ImageView picture;
File destination;
String imagePath;
private Uri mSharedUri = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvPath = (TextView) findViewById(R.id.idTvPath);
picture = (ImageView) findViewById(R.id.idIvImage);
String name = "test_image";
destination = new File(Environment.getExternalStorageDirectory(), name + ".jpg");
fixPermissions();
fixPermissions2();
Button click = (Button) findViewById(R.id.idBtnTakePicture);
click.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri contentUriToShare = FileProvider.getUriForFile(getApplicationContext(),
"com.weebly.stevelosk.fileprovider", destination);
mSharedUri = contentUriToShare;
intent.putExtra(MediaStore.EXTRA_OUTPUT, mSharedUri);
startActivityForResult(intent, REQUEST_IMAGE);
}
});
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
File file = new File(Environment.getExternalStorageDirectory(), "test_image.jpg");
//Toast.makeText(MainActivity.this, file.getPath(), Toast.LENGTH_LONG).show();
Uri path = Uri.fromFile(file);
if (file.exists()) {
Toast.makeText(MainActivity.this, file.getPath(), Toast.LENGTH_LONG).show();
Uri contentUriToShare = FileProvider.getUriForFile(getApplicationContext(),
"com.weebly.stevelosk.fileprovider", file);
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(contentUriToShare, "image/*");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
getApplicationContext().startActivity(intent);
} catch (ActivityNotFoundException e) {
}
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if( requestCode == REQUEST_IMAGE && resultCode == Activity.RESULT_OK ){
try {
FileInputStream in = new FileInputStream(destination);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 10;
imagePath = destination.getAbsolutePath();
tvPath.setText(imagePath);
Bitmap bmp = BitmapFactory.decodeStream(in, null, options);
picture.setImageBitmap(bmp);
} catch (FileNotFoundException e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
else{
tvPath.setText("Request cancelled");
Integer reqCode = (Integer) requestCode;
String RC = reqCode.toString();
Toast.makeText(getApplicationContext(), RC,
Toast.LENGTH_LONG).show();
}
}
private void fixPermissions() {
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.READ_EXTERNAL_STORAGE)) {
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA},
MY_PERMISSIONS_REQUEST_CAMERA);
}
}
}
private void fixPermissions2() {
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_CAMERA);
}
}
}
}
android清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.weebly.stevelosk.cameratestapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="23" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-feature android:name="android.hardware.camera" />
<uses-feature
android:name="android.hardware.camera.any"
android:required="true" />
<application
android:icon="@drawable/ic_launcher_background"
android:label="@string/app_name" >
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.weebly.stevelosk.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
<activity
android:name="com.weebly.stevelosk.cameratestapp.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
文件路径:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images" path="." />
</paths>
我可能会忽略一些简单的事情,但我会感激任何帮助!谢谢!