Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

java - 在Android 7.0版中将文件路径转换为Android中的Uri [重复](Convert a file path to Uri in Android for android 7.0 [duplicate])

Hello friend anyone know convert this code from file to uri for android 7.0 i work 6.0 device but not work 7.0 help me here is my code i used this code for sharing images its work below 7.0 device but above 7.0 and 7.0 not working?

(你好朋友有人知道将此代码从文件转换为android 7.0的uri我工作6.0设备但不工作7.0帮助我在这里是我的代码我用此代码共享图像在7.0设备以下但在7.0和7.0以上无法工作的情况吗?)

public void shareContent(){




 Bitmap bitmap =getBitmapFromView(imageView);
    try {
        File file = new File(mContext.getExternalCacheDir(),"logicchip.png");
        FileOutputStream fOut = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
        fOut.flush();
        fOut.close();
        file.setReadable(true, false);
        final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(Intent.EXTRA_TEXT, "");
        intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
        intent.setType("image/png");
       mContext.startActivity(Intent.createChooser(intent, "Share image via"));
    } catch (Exception e) {
        e.printStackTrace();
    }

}

private Bitmap getBitmapFromView(View view) {
    Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(returnedBitmap);
    Drawable bgDrawable =view.getBackground();
    if (bgDrawable!=null) {
        bgDrawable.draw(canvas);
    }   else{
        canvas.drawColor(Color.WHITE);
    }
    view.draw(canvas);
    return returnedBitmap;
}

//Mainfest i used
 <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:grantUriPermissions="true"
            android:exported="false">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/filepaths" />
        </provider>

//in res/xml/filpath

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <cache-path name="shared_images" path="images/"/>
</paths>
  ask by joshua translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
 Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {

    if (photoFile != null) {
        Uri photoURI = FileProvider.getUriForFile(context,
                DBConnect.FILEPROVIDER,
                photoFile);




        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,photoURI);
        startActivityForResult(takePictureIntent, REQUEST_TAKE_CAMIRA);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...