我创建了一个按钮,显示从相机拍摄照片或从照片库中选择图像的选项。图像将定向到同一活动中的imageview
。当相机用于获取图像时,它按预期工作,而应用程序在从图库中选择图像后停止,而不是在imageview
中显示。我无法弄清楚是否由于图像解析缩略图或者代码中缺少某些内容?
主要活动:
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class MainActivity extends Activity {
ImageView vitb;
Button btcm;
static final int REQUEST_IMAGE_CAPTURE = 1;
static final int REQUEST_IMAGE_SELECT = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vitb = (ImageView) findViewById(R.id.imageView);
btcm = (Button) findViewById(R.id.btn_cam);
btcm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectImage();
}
});
}
private void selectImage() {
final CharSequence[] options = { "Take Photo", "Choose from
Gallery","Cancel" };
AlertDialog.Builder builder = new
AlertDialog.Builder(MainActivity.this);
builder.setTitle("Add Photo!");
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (options[item].equals("Take Photo"))
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,REQUEST_IMAGE_CAPTURE);
}
else if (options[item].equals("Choose from Gallery"))
{
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent,REQUEST_IMAGE_SELECT);
}
else if (options[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent
data) {
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
Bundle bundle = data.getExtras();
Bitmap bmp = (Bitmap)bundle.get("data");
vitb.setImageBitmap(bmp);
}
else if (requestCode == 2) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
}
}
logcat的:
01-21 22:53:56.345 20914-20914/com.dot.hackathon E/AndroidRuntime: FATAL
EXCEPTION: main
Process:
com.dot.hackathon, PID: 20914
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null,
request=2, result=-1, data=Intent {
dat=content://media/external/images/media/44167 }} to activity
{com.dot.hackathon/com.dot.hackathon.MainActivity}:
java.lang.SecurityException: Permission Denial: reading
com.android.providers.media.MediaProvider uri
content://media/external/images/media/44167 from pid=20914, uid=10118
requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
at
android.app.ActivityThread.deliverResults(ActivityThread.java:3782)
at
android.app.ActivityThread.handleSendResult(ActivityThread.java:3825)
at
android.app.ActivityThread.access$1500(ActivityThread.java:164)
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1511)
at
android.os.Handler.dispatchMessage(Handler.java:102)
at
android.os.Looper.loop(Looper.java:159)
at
android.app.ActivityThread.main(ActivityThread.java:5541)
at
java.lang.reflect.Method.invoke(Native Method)
at
java.lang.reflect.Method.invoke(Method.java:372)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:964)
at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)
Caused by:
java.lang.SecurityException: Permission Denial: reading
com.android.providers.media.MediaProvider uri
content://media/external/images/media/44167 from pid=20914, uid=10118
requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
at
android.os.Parcel.readException(Parcel.java:1559)
at
android.database.DatabaseUtils.readExceptionFromParcel
(DatabaseUtils.java:185)
at
android.database.DatabaseUtils.readExceptionFromParcel
(DatabaseUtils.java:137)
at
android.content.ContentProviderProxy.query(ContentProviderNative.java:421)
at
android.content.ContentResolver.query(ContentResolver.java:527)
at
android.content.ContentResolver.query(ContentResolver.java:471)
at
com.dot.hackathon.MainActivity.onActivityResult(MainActivity.java:125)
at
android.app.Activity.dispatchActivityResult(Activity.java:6309)
at
android.app.ActivityThread.deliverResults(ActivityThread.java:3778)
at
android.app.ActivityThread.handleSendResult(ActivityThread.java:3825)
at
android.app.ActivityThread.access$1500(ActivityThread.java:164)
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1511)
at
android.os.Handler.dispatchMessage(Handler.java:102)
at
android.os.Looper.loop(Looper.java:159)
at
android.app.ActivityThread.main(ActivityThread.java:5541)
at
java.lang.reflect.Method.invoke(Native Method)
at
java.lang.reflect.Method.invoke(Method.java:372)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:964)
at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)
01-21 22:53:56.445 20914-20914/com.dot.hackathon I/Process: Sending signal.
PID: 20914 SIG: 9
01-21 22:53:56.445 20914-20914/com.dot.hackathon V/Process: killProcess
[20914]
Callers=com.android.internal.os.RuntimeInit$UncaughtHandler.
uncaughtException:99 java.lang.ThreadGroup.uncaughtException:693
java.lang.ThreadGroup.uncaughtException:690 <bottom of call stack>
答案 0 :(得分:0)
试试这个
在清单文件中添加此权限。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Run Time Permissions Requesting
如何查看运行时权限,请查看下面的代码片段
<强>步骤1 强>
private boolean checkPermission() {
int result = ContextCompat.checkSelfPermission(Your_Activity.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (result == PackageManager.PERMISSION_GRANTED) {
return true;
} else {
return false;
}
}
<强>第二步强>
private void requestPermission() {
if (ActivityCompat.shouldShowRequestPermissionRationale(Your_Activity.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
//Here you can explain why you need this permission
//Explain here why you need this permission
} else {
ActivityCompat.requestPermissions(Your_Activity.this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);
}
}
<强>步骤3 强>
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.i("TAG", "Permission Granted");
} else {
Log.i("TAG", "Permission Denied.");
}
break;
}
}