2

Im attempting to save the actual image from my applications resources to a file on the SD card so it is accessible by the messaging application but I'm getting the error of - The method write(byte[]) in the type FileOutputStream is not applicable for the arguments (Drawable) - where its supposed to save it. I believe that I'm not using the appropriate method to save the actual image itself and I'm having difficulty finding a solution.

                        // Selected image id
                int position = data.getExtras().getInt("id");
                ImageAdapter imageAdapter = new ImageAdapter(this);
                ChosenImageView.setImageResource(imageAdapter.mThumbIds[position]);
                Resources res = getResources();
                Drawable drawable = res.getDrawable(imageAdapter.mThumbIds[position]); //(imageAdapter.mThumbIds[position]) is the resource ID
                try {
                    File file = new File(dataFile, FILENAME);
                    file.mkdirs();
                    FileOutputStream fos = new FileOutputStream(file);
                    fos.write(drawable); // supposed to save the image here, getting the error at fos.write
                    fos.close();
                }catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
              }
            }
        }    
4

1 回答 1

2

您的错误确实说明了一切,fileOuputStream 不能采用 Drawable,它需要一个字节数组。

所以你需要从你的 Drawable 中获取字节。

尝试:

Drawable drawable = res.getDrawable(imageAdapter.mThumbIds[position]);
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] bitmapdata = stream.toByteArray();

……

fos.write(bitmapdata);

当你在做的时候,把fos.close()最后的块

于 2012-11-03T17:53:09.067 回答