写真を撮影して電話機の内蔵ストレージに保存してImageViewにセットしようとすると数時間です。イメージをキャプチャしてストレージに保存し、ImageViewで表示する方法は?
私はStackoverflowとyoutubeからいくつかのコードを試しましたが、FileUriExposedExceptionを取得しています...私は理由を知りません。多分あなたはアイディアを持っています。
file://の代わりにcontent://を使用する必要があることを読んだが、これが私の問題の理由ですか?
イム取得:あなたのtargetSdkVersionが24以上の場合
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!");
}
}
に以下のコードの行を変更することです/ Camera/FileProvider – CommonsWare
'どこから写真を撮る?カメラ? –
はいカメラから – skm