Reputation: 571
Is there any way to create file from image drawable to send it via MultypartEntity
? I having error all the time.
fileUri = Uri.parse("android.resource://com.wellbread/drawable/placeholder_avatar.png");
mCurrentPhotoPath = fileUri.toString();
java.io.FileNotFoundException: android.resource:/com.wellbread/drawable/placeholder_avatar.png: open failed: ENOENT (No such file or directory)
08-25 12:01:53.134 3663-3684/? W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:456)
Upvotes: 2
Views: 15935
Reputation: 1092
You have to get Bitmap from drawable:
public static Bitmap decodeAndSetWidthHeight(Resources res, int resId, int reqWidth, int reqHeight){
Bitmap btm = BitmapHelper.decodeSampledBitmapFromResource(res, resId, reqWidth, reqHeight);
return decodeAndSetWidthHeight(btm, reqWidth, reqHeight);
}
Then you can create file from bitmap
public static File bitmapToFile(Bitmap bitmap){
File outFile = FileHelper.getImageFilePNG();
FileOutputStream out = null;
try {
out = new FileOutputStream(outFile);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return outFile;
}
UPDATE (sorry, forget to provide some methods):
public static File getImageFilePNG() {
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File(sdCard.getAbsolutePath() + "/any name folder");
dir.mkdirs();
String fileName = String.format("%d.png", System.currentTimeMillis());
return new File(dir, fileName);
}
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
Log.d("ANT", "options.inSampleSize : " + options.inSampleSize);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
public static Bitmap decodeAndSetWidthHeight(Bitmap btm, int reqWidth, int reqHeight){
Matrix m = new Matrix();
RectF inRect = new RectF(0, 0, btm.getWidth(), btm.getHeight());
RectF outRect = new RectF(0, 0, reqWidth, reqHeight);
m.setRectToRect(inRect, outRect, Matrix.ScaleToFit.FILL);
float[] values = new float[9];
m.getValues(values);
return Bitmap.createScaledBitmap(btm, (int) (btm.getWidth() * values[0]), (int) (btm.getHeight() * values[4]), true);
}
Upvotes: 1
Reputation: 24114
Try the following sample code:
Drawable drawable = getResources().getDrawable(R.drawable.ic_action_home);
if (drawable != null) {
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
final byte[] bitmapdata = stream.toByteArray();
String url = "http://10.0.2.2/api/fileupload";
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
// Add binary body
if (bitmapdata != null) {
ContentType contentType = ContentType.create("image/png");
String fileName = "ic_action_home.png";
builder.addBinaryBody("file", bitmapdata, contentType, fileName);
httpEntity = builder.build();
...
}
...
}
Upvotes: 5
Reputation: 1603
You can open an InputStream from your drawable resource using following code:
try
{
File f=new File("your file name");
//id is some like R.drawable.b_image
InputStream inputStream = getResources().openRawResource(id);
OutputStream out=new FileOutputStream(f);
byte buf[]=new byte[1024];
int len;
while((len=inputStream.read(buf))>0)
out.write(buf,0,len);
out.close();
inputStream.close();
}
catch (IOException e){}
}
Upvotes: 1
Reputation: 724
try this:
try
{
File f=new File("file name");
InputStream inputStream = getResources().openRawResource(id); // id drawable
OutputStream out=new FileOutputStream(f);
byte buf[]=new byte[1024];
int len;
while((len=inputStream.read(buf))>0)
out.write(buf,0,len);
out.close();
inputStream.close();
}
catch (IOException e){}
}
Upvotes: 3