Reputation: 816
I have a set of icons stored in the drawable folder.
Based on users selection I'll be posting these icons to the backend.
When I try to retrieve the path for these drawables, I get the following error
HTTP FAILED: java.io.FileNotFoundException: (No such file or directory)
This is how I am getting the path
public String getURLForResource (int resourceId) {
return Uri.parse("android.resource://"+ BuildConfig.APPLICATION_ID+"/" +resourceId).toString();
}
File fileToUpload = new File(getURLForResource(R.drawable.icon));
I have referred the following link says that it's not possible to retrieve the path for a drawable.
Any idea on how I can solve this problem?
Upvotes: 0
Views: 198
Reputation: 58934
As solution you can write your drawable to file.
Bitmap bm = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.your_drawable);
String path = Environment.getExternalStorageDirectory().toString();
File file = new File(path, "image" + System.currentTimeMillis() + ".jpg");
try (FileOutputStream out = new FileOutputStream(file)) {
bm.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush(); // Not really required
out.close(); // do not forget to close the stream
} catch (IOException e) {
e.printStackTrace();
}
You can use file now.
Upvotes: 1