Issue
I have the following configuration: a FileProvider
from one application that provider other application files of type drawables and other text files that contain json structure.
Now all this configuration work and I receive a content uri (content://...) from one application at the other for both types of files.
What I don't know is how can I recognize on the receiver application what type of file it's received (drawable or json .txt) and how to convert properly the uri to one of this file types.
This is the current activity of the receiver application:
public class MainActivity extends Activity {
ListView listView;
Cursor cursor;
Button bShowImageFiles, bShowJsonFiles;
Context mContext;
//TODO: Densities
static final String DENSITY_LOW = "ldpi";
static final String DENSITY_MEDIUM = "mdpi";
static final String DENSITY_HIGH = "hdpi";
static final String DENSITY_XHIGH = "xhdpi";
static final String DENSITY_XXHIGH = "xxhdpi";
public static final String FILES = "video";
public static final String PROVIDER_AUTHORITY = "some.authority";
public static final Uri CONTENT_URI = Uri.parse("content://" + PROVIDER_AUTHORITY + "/");
public static Uri FILES_URI = Uri.parse("content://" + PROVIDER_AUTHORITY + "/" + FILES + "/");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
bShowImageFiles = (Button) findViewById(R.id.bShowImageFiles);
bShowJsonFiles = (Button) findViewById(R.id.bShowJsonFiles);
listView = (ListView) findViewById(R.id.listView1);
final String[] from = { "_id", "fileName" };
final int[] to = { R.id.textViewID, R.id.textFileName };
bShowImageFiles.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
configureFilesUri(getImageDirString(mContext));
cursor = getContentResolver().query(getImageFilesUri(mContext), null, null, null, null);
SimpleCursorAdapter sAdapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.row, cursor, from, to);
listView.setAdapter(sAdapter);
listView.setOnItemClickListener(new ListListener());
}
});
bShowJsonFiles.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
configureFilesUri(getJsonDirString());
cursor = getContentResolver().query(getJsonFilesUri(), null, null, null, null);
SimpleCursorAdapter sAdapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.row, cursor, from, to);
listView.setAdapter(sAdapter);
listView.setOnItemClickListener(new ListListener());
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class ListListener implements OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
cursor.moveToPosition(position);
String name = cursor.getString(cursor.getColumnIndex("fileName"));
Uri fileUri = Uri.parse(FILES_URI + name);
Log.i("DEBUG:", "path: " + fileUri);
String type = getMimeType(name);
try {
InputStream inputStream = getContentResolver().openInputStream(fileUri);
convertInputStreamToFile(inputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
public static String getMimeType(String uri) {
String extension = uri.substring(uri.lastIndexOf("."));
String mimeTypeMap = MimeTypeMap.getFileExtensionFromUrl(extension);
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(mimeTypeMap);
return mimeType;
}
public static String getImageDirString(Context aContext) {
return "drawable-" + getDeviceDpiName(aContext);
}
public static String getJsonDirString() {
return "json";
}
public static Uri getImageFilesUri(Context aContext) {
return Uri.parse("content://" + PROVIDER_AUTHORITY + "/" + getImageDirString(aContext) + "/");
}
public static Uri getJsonFilesUri() {
return Uri.parse("content://" + PROVIDER_AUTHORITY + "/" + getJsonDirString() + "/");
}
public static String getDeviceDpiName(Context aContext) {
int density = aContext.getResources().getDisplayMetrics().densityDpi;
String dpi;
switch(density)
{
case DisplayMetrics.DENSITY_LOW:
dpi = DENSITY_LOW;
break;
case DisplayMetrics.DENSITY_MEDIUM:
dpi = DENSITY_MEDIUM;
break;
case DisplayMetrics.DENSITY_HIGH:
dpi = DENSITY_HIGH;
break;
case DisplayMetrics.DENSITY_XHIGH:
dpi = DENSITY_XHIGH;
break;
case DisplayMetrics.DENSITY_XXHIGH:
dpi = DENSITY_XXHIGH;
break;
default:
dpi = DENSITY_HIGH;
break;
}
return dpi;
}
private void configureFilesUri(String filesPath) {
FILES_URI = Uri.parse("content://" + PROVIDER_AUTHORITY + "/" + filesPath + "/");
}
private void convertInputStreamToFile( InputStream aInputStream) {
File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File (root.getAbsolutePath() + "/kibo");
dir.mkdirs();
File file = new File(dir, "bubu.png");
try {
FileOutputStream os = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int bytesRead;
//read from is to buffer
while((bytesRead = aInputStream.read(buffer)) !=-1){
os.write(buffer, 0, bytesRead);
}
aInputStream.close();
//flush OutputStream to write any buffered data to file
os.flush();
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.i("DEBUG", "******* File not found. Did you" +
" add a WRITE_EXTERNAL_STORAGE permission to the manifest?");
} catch (IOException e) {
e.printStackTrace();
}
}
Explanation: So you can see that currently I transfer the Uri
to InputStream
that in it's turn gets converted to file using an FileOutputStream
. And this works as long as I get drawables files from the second project. But if I get a json file the application crashes because the InputStream
I getting from this file is null for some reason.
UPDATE (19.11.14): After additional research I realized that the reason that I can't get an InputStream for the .txt files is because they are getting compressed at compile time (as opposed to the drawable .png files which is already a compressed format)
So the question is: How to prevent gradle from compressing .txt files that are located in the assets folder?
Solution
It turns out that it was the compression after all. Adding the following:
aaptOptions {
noCompress 'txt', '.txt', 'json', '.json'
}
to the build.gradle file allows you to disable compression on specific file types in your project in the assets folder. You can get more information in the following link:
Answered By - Emil Adz
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.