2

アプリケーションリソースからSDカード上のファイルに実際の画像を保存して、メッセージングアプリケーションからアクセスできるようにしようとしていますが、次のエラーが発生します-FileOutputStreamタイプのwrite(byte [])メソッドは適用できません引数用(Drawable)-保存する場所。実際の画像自体を保存するための適切な方法を使用しておらず、解決策を見つけるのに苦労していると思います。

                        // 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 に答える