我幾個小時試圖拍照並將其保存到手機的內部存儲並將其設置爲ImageView。如何捕獲圖像並將其保存在存儲器中並在ImageView中顯示?
我嘗試了幾個代碼從Stackoverflow和YouTube但只是得到FileUriExposedException ...我不知道爲什麼。也許你有一個想法。
我讀過你應該使用content://而不是file://,但這是我的問題的原因嗎?
即時得到:
Caused by: android.os.FileUriExposedException: file:///storage/emulated/0/Pictures/test.jpg exposed beyond app through ClipData.Item.getUri()
at android.os.StrictMode.onFileUriExposed(StrictMode.java:1799)
at android.net.Uri.checkFileUriExposed(Uri.java:2346)
at android.content.ClipData.prepareToLeaveProcess(ClipData.java:845)
at android.content.Intent.prepareToLeaveProcess(Intent.java:8941)
at android.content.Intent.prepareToLeaveProcess(Intent.java:8926)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1517)
at android.app.Activity.startActivityForResult(Activity.java:4225)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79)
at android.app.Activity.startActivityForResult(Activity.java:4183)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
at ibas.orosol.BildActivity.takePicture(BildActivity.java:86)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Here's我的活動
final private int REQUEST_CODE_ASK_PERMISSION = 123; //kann jegliche Zahl sein. Wir benutzens halt später
final private int CAMERA_REQUEST = 555; //kann jegliche Zahl sein. Wir benutzens halt später
private File imageFile;
private ImageView mImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bild);
getSupportActionBar().setTitle("");
getSupportActionBar().setBackgroundDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.kopf_app, null));
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mImageView = (ImageView) findViewById(R.id.imageView);
// wenn die Permission NICHT gegeben wurde... und was wir dann machen kommt in diesem Block
if(ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED){
if(ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)){
//Hier können wir eintragen, wieso wir eigentlich die Permission brauchen/verlangen
}else{
//Permission anfragen
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE_ASK_PERMISSION);
}
}
//---------------------------------------------------------------------------------------------
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_CODE_ASK_PERMISSION);
}
public void takePicture(View view) throws IOException {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = getFile();
imageFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "test.jpg");
Uri tempUri = Uri.fromFile(imageFile);
String path = "sdcard/orosol/captured_image.jpg";
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, tempUri);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String path = "sdcard/orosol/captured_image.jpg";
if(imageFile.exists()){
Log.i("imagefile", "exists in: " + imageFile.getAbsolutePath());
}else{
Log.i("imagefile", "existiert nicht!");
}
}
https://github.com/commonsguy/cw-omnibus/tree/master/Camera/FileProvider – CommonsWare
'從哪拍攝圖片?相機? –
是從相機 – skm