无法获取捕获图像的URI。

huangapple go评论98阅读模式
英文:

Unable to get captured image uri

问题

Button b = findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(i, 2);
    }
});

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Toast.makeText(Authentication.this, "" + data.getData(), Toast.LENGTH_LONG).show();
}
英文:

I am trying to get Captured image Uri but not getting this below code how I get uri. Toast message giving me null result

 Button b=findViewById(R.id.button);
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(i,2);
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Toast.makeText(Authentication.this,""+data.getData(),Toast.LENGTH_LONG).show();
}

答案1

得分: 0

请尝试以下代码:

if (requestCode == CAPTURE_IMAGE &&
    resultCode == RESULT_OK) {
    if (data != null) {
        Bitmap bitmap = (Bitmap) data.getExtras().get("data");
        
        Uri imageUri = getImageUri(getApplicationContext(), bitmap);
    }
}

调用以下方法以从位图获取 URI:

public Uri getImageUri(Context inContext, Bitmap myImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    myImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = Images.Media.insertImage(inContext.getContentResolver(), 
        myImage, "Title", null);
    return Uri.parse(path);
}
英文:

Try this:

if (requestCode == CAPTURE_IMAGE && 
                              resultCode == RESULT_OK) {
        if (data != null) {
        Bitmap bitmap = (Bitmap) data.getExtras().get("data");
        
        Uri imageUri = getImageUri(getApplicationContext(), bitmap);
        }
    }

Call this method to get uri fom bitmap:

public Uri getImageUri(Context inContext, Bitmap myImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    myImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = Images.Media.insertImage(inContext.getContentResolver(), 
    myImage, "Title", null);
    return Uri.parse(path);
}

答案2

得分: 0

从Android版本LOLLIPOP开始,出于安全原因,您无法直接获取URI,请尝试以下操作:

private static Uri getSharableImageUri(Context context) {
    File outputDir = context.getCacheDir();
    File imagePath = new File(outputDir, context.getString(R.string.app_name));
    if (!imagePath.exists()) {
        imagePath.mkdir();
    }
    File tempFile = new File(imagePath, "image.jpg");
    Uri uri = null;
    try {
        if (!tempFile.exists()) {
            tempFile.createNewFile();
        }
        uri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", tempFile);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return uri;
}

在按钮的点击监听器中使用以下代码:

Button b = findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        try {
            String state = Environment.getExternalStorageState();
            if (Environment.MEDIA_MOUNTED.equals(state)) {
                mImageCaptureUri = getSharableImageUri(context);
            }
            if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP) {
                intent.setClipData(ClipData.newRawUri("", mImageCaptureUri));
            }
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
            intent.putExtra("return-data", true);
            startActivityForResult(intent, 2);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
});

在onActivityResult方法中,如果结果成功,获取URI:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK && requestCode == 2) {
        Toast.makeText(Authentication.this, "" + data.getData(), Toast.LENGTH_LONG).show();
    }
}
英文:

From Android version LOLLIPOP you can not get the URI directly for security reasons try this-

    private static Uri getSharableImageUri(Context context) {
File outputDir = context.getCacheDir();
File imagePath = new File(outputDir, context.getString(R.string.app_name));
if (!imagePath.exists()) {
    imagePath.mkdir();
}
File tempFile = new File(imagePath, &quot;image.jpg&quot;);
Uri uri = null;
try {
    if (!tempFile.exists()) {
        tempFile.createNewFile();
    }
    uri = FileProvider.getUriForFile(context, context.getPackageName() + &quot;.fileprovider&quot;, tempFile);
} catch (Exception e) {
    e.printStackTrace();
}
return uri;
}

And in your button's click listener use this-

     Button b=findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
       Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
try {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {

        mImageCaptureUri = getSharableImageUri(context);
    }
    if (Build.VERSION.SDK_INT &lt;= Build.VERSION_CODES.LOLLIPOP) {
        intent.setClipData(ClipData.newRawUri(&quot;&quot;, mImageCaptureUri));

    }
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
    intent.putExtra(&quot;return-data&quot;, true);
    startActivityForResult(intent, 2);

} catch (Exception e) {
   e.printStackTrace();
}
    }
});

}

And get the URI in onActivityResult if the result is successful -

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
            if (resultCode == Activity.RESULT_OK &amp;&amp; requestCode==2) {
    Toast.makeText(Authentication.this,&quot;&quot;+data.getData(),Toast.LENGTH_LONG).show();
    }

huangapple
  • 本文由 发表于 2020年4月10日 14:45:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/61135279.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定