图像存储在内部存储中,同时在Android中共享图像



我在该应用程序中创建应用程序,正在共享图像等布局。 虽然我共享正在将布局转换为图像并在社交网络中共享,但问题是图像存储在内部存储中,共享图像后如何删除。

 bitmap = Bitmap.createBitmap(itemView.getWidth(), itemView.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            itemView.draw(canvas);
            try {
                // output = new FileOutputStream(Environment.getExternalStorageDirectory() + "/path/to/file.png");
                file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png");
                image_path = file.getAbsolutePath();

                file.getParentFile().mkdirs();
                FileOutputStream out = new FileOutputStream(file);
                bitmap.compress(Bitmap.CompressFormat.PNG, 50, out);
                out.close();
                bmpUri = Uri.fromFile(file);
                sharePost();

  public void sharePost() {
    try {
        Resources resources = context1.getResources();
        final String photoUri = MediaStore.Images.Media.insertImage(
                context1.getContentResolver(), image_path, null, null);
        Intent emailIntent = new Intent();
        emailIntent.setAction(Intent.ACTION_SEND);
        emailIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
        emailIntent.setType("image/*");
        PackageManager pm = context1.getPackageManager();
        Intent openInChooser = Intent.createChooser(emailIntent, "Share via Oddcast");
        List<ResolveInfo> resInfo = pm.queryIntentActivities(emailIntent, 0);
        List<LabeledIntent> intentList = new ArrayList<LabeledIntent>();
        for (int i = 0; i < resInfo.size(); i++) {
            // Extract the label, append it, and repackage it in a LabeledIntent
            ResolveInfo ri = resInfo.get(i);
            String packageName = ri.activityInfo.packageName;
            if (packageName.contains("android.email")) {
                emailIntent.setPackage(packageName);
            } else {
                emailIntent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
                if (packageName.contains("twitter")) {
                    emailIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
                } else if (packageName.contains("facebook")) {
                    emailIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
                } else if (packageName.contains("mms")) {
                    emailIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
                } else if (packageName.contains("android.gm")) {
                    emailIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
                } else if (packageName.contains("whatsapp")) {
                    emailIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
                }else if (packageName.contains("com.google.android.apps.plus")) {
                    emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(photoUri));
                }
                else{
                    emailIntent.putExtra(Intent.EXTRA_STREAM, MyCustomAdapter.bmpUri);
                }
                intentList.add(new LabeledIntent(emailIntent, packageName, ri.loadLabel(pm), ri.icon));
            }
        }
        // convert intentList to array
        LabeledIntent[] extraIntents = intentList.toArray(new LabeledIntent[intentList.size()]);
        openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
        context1.startActivity(openInChooser);
        Log.e("start", "start");
    }catch (SecurityException e) {
        Toast.makeText(context1, "Please check your permissions settings.Permission issue.", Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }  catch (Exception e) {
        Toast.makeText(context1, "Please check your permissions settings.Permission issue.", Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }
    file.delete();
    Log.e("emd","end");
    //context1.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(String.valueOf(bmpUri)))));
}

尝试调用 startActivityForResult,而不是 startActivity。覆盖 onActivityResult 方法并删除您创建的图像文件

相关内容

  • 没有找到相关文章

最新更新