20170305

res->raw->test.txt

 

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.example.wata.myapplication">

    <application

        android:allowBackup="true"

        android:icon="@mipmap/ic_launcher"

        android:label="@string/app_name"

        android:supportsRtl="true"

        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

        <provider

            android:name=".FileProvider"

            android:authorities="com.example.wata.myapplication.fileProvider"

            android:exported="false">

        </provider>

    </application>

</manifest>

//

package com.example.wata.myapplication;

import android.content.ContentProvider;

import android.content.ContentValues;

import android.content.Context;

import android.database.Cursor;

import android.net.Uri;

import android.os.ParcelFileDescriptor;

import java.io.File;

import java.io.FileNotFoundException;

 

public class FileProvider extends ContentProvider {

    public boolean onCreate() {return true;}

    public ParcelFileDescriptor openFile(Uri uri, String mode)throws FileNotFoundException {

        Context context = getContext();

        String path = context.getFilesDir()+"/"+uri.getLastPathSegment();

        File file=new File(path);

        ParcelFileDescriptor parcelFileDescriptor=ParcelFileDescriptor.open(file,ParcelFileDescriptor.MODE_READ_ONLY);

        return parcelFileDescriptor;

    }

    public Cursor query(Uri uri, String[] projection, String selection,String[] selectionArgs, String sortOrder) {return null;}

    public Uri insert(Uri uri, ContentValues values) {return null;}

    public int update(Uri uri, ContentValues values,String selection, String[] selectionArgs) {return 0;}

    public int delete(Uri uri, String selection,String[] selectionArgs) {return 0;}

    public String getType(Uri uri) {return null;}

}

//

package com.example.wata.myapplication;

 

import android.app.Activity;

import android.net.Uri;

import android.os.Bundle;

import android.view.MotionEvent;

import android.widget.Toast;

import java.io.ByteArrayOutputStream;

import java.io.InputStream;

 

public class MainActivity extends Activity{

        protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_main);

        }

        public boolean onTouchEvent(MotionEvent event) {

            try {

                Uri uri = Uri.parse("content://com.example.wata.myapplication.fileProvider/test");

                InputStream inputStream = getContentResolver().openInputStream(uri);

                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

                byte[] w = new byte[1024];

                while (true) {

                        int size = inputStream.read(w);

                        if (size <= 0) break;

                        outputStream.write(w, 0, size);

                };

                 String str = new String(outputStream.toByteArray());

                 Toast.makeText(this, str, Toast.LENGTH_LONG).show();

                 inputStream.close();

                 outputStream.close();

            } catch (Exception e) {}

                return true;

        }

}