0

I am trying to open a private file using another application. I created ContentProvider for the same as below. But I get following error:

06-05 12:57:20.791: E/AndroidRuntime(21922): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.contentprovider/com.android.contentprovider.MainActivity}: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=content://com.android.contentprovider//data/data/com.android.contentprovider/files/Sample.txt typ=/text* }

My code in the main activity is:

try {
File newFile = new File("Sample.txt");
FileOutputStream fileOutputStream = openFileOutput(newFile.toString(), MainActivity.MODE_PRIVATE);
    } catch (Exception e) {
        Toast.makeText(MainActivity.this, "Failed", Toast.LENGTH_SHORT).show();
    }

    Uri uri = Uri.parse("content://com.android.contentprovider/" + this.getFilesDir().toString() + "/Sample.txt");

    Intent intent = new Intent();
    intent.setAction(android.content.Intent.ACTION_VIEW);
    intent.setDataAndType(uri, "/text*");
    startActivity(intent);

My contentprovider file:

 package com.android.contentprovider;

import java.io.File;
import java.io.FileNotFoundException;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.ParcelFileDescriptor;

public class MyProvider extends ContentProvider {

@Override
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
    File privateFile = new File(getContext().getFilesDir(), uri.getPath());
    return ParcelFileDescriptor.open(privateFile, ParcelFileDescriptor.MODE_READ_ONLY);
}

@Override
public int delete(Uri arg0, String arg1, String[] arg2) {
    return 0;
}

@Override
public String getType(Uri arg0) {
    return null;
}

@Override
public Uri insert(Uri arg0, ContentValues arg1) {
    return null;
}

@Override
public boolean onCreate() {
    return false;
}

@Override
public Cursor query(Uri arg0, String[] arg1, String arg2, String[] arg3,
        String arg4) {
    return null;
}

@Override
public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) {
    return 0;
}

}

And I have added provider in the manifest

<provider 
        android:name=".MyProvider" 
        android:authorities="com.android.contentprovider" 
        android:exported="true" />
  • first verify your manifest file,is it your MainActivity name correctly mentioned ? and then your MainActivity extends with activity class. – prakash Jun 05 '14 at 13:24

2 Answers2

0

You have not implemented an activity that supports ACTION_VIEW for your desired Uri and (malformed) MIME type. A ContentProvider is not an Activity.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

My idiotic mistake was "/video*" which should have been "video/*"

Still some problem existed. So I used the FileProvider from v4 library

Check it out here

Community
  • 1
  • 1