Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 要将bmp保存到sd卡中,并在以后检索它吗_Android_Android Camera_Sharedpreferences_Android Sdcard_Android Bitmap - Fatal编程技术网

Android 要将bmp保存到sd卡中,并在以后检索它吗

Android 要将bmp保存到sd卡中,并在以后检索它吗,android,android-camera,sharedpreferences,android-sdcard,android-bitmap,Android,Android Camera,Sharedpreferences,Android Sdcard,Android Bitmap,我正在制作一个应用程序,到目前为止我已经完成了 public class TrafficAdd extends Activity{ Button take,upload; EditText sub,details; Bitmap bmp; Intent i; ImageView iv; final static int cameraData=0; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Au

我正在制作一个应用程序,到目前为止我已经完成了

public class TrafficAdd extends Activity{
Button take,upload;
EditText sub,details;
Bitmap bmp;
Intent i;
ImageView iv;
final static int cameraData=0;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.trafficadd);
InputStream is=getResources().openRawResource(R.drawable.green);
bmp=BitmapFactory.decodeStream(is);
take=(Button) findViewById(R.id.trafficcamera);
upload=(Button) findViewById(R.id.trafficupload);
sub=(EditText) findViewById(R.id.eTtrafficSub);
details=(EditText) findViewById(R.id.eTtrafficDetails);
iv=(ImageView) findViewById(R.id.trafficpic);

take.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        i=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(i,cameraData);


    }
});

 }
  @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK){
    Bundle extras=data.getExtras();
    bmp=(Bitmap)  extras.get("data");
    iv.setImageBitmap(bmp);
}
   }

 }
我正在从相机拍摄照片,我可以将其设置为位图bmp。现在我需要将此bmp保存在SD卡中的某个位置,然后检索它以供查看。
请帮忙

在SD卡上存储图像:

public boolean storeImage(Bitmap imageData, String filename) {
    //get path to external storage (SD card)
    String iconsStoragePath = Environment.getExternalStorageDirectory() + "/myAppDir/myImages/"
    File sdIconStorageDir = new File(iconsStoragePath);

    //create storage directories, if they don't exist
    sdIconStorageDir.mkdirs();

    try {
        String filePath = sdIconStorageDir.toString() + filename;
        FileOutputStream fileOutputStream = new FileOutputStream(filePath);

        BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);

        //choose another format if PNG doesn't suit you
        imageData.compress(CompressFormat.PNG, 100, bos);

        bos.flush();
        bos.close();

    } catch (FileNotFoundException e) {
        Log.w("TAG", "Error saving image file: " + e.getMessage());
        return false;
    } catch (IOException e) {
        Log.w("TAG", "Error saving image file: " + e.getMessage());
        return false;
    }

    return true;
}
File f = new File(android.os.Environment.getExternalStorageDirectory()+ "/myAppDir/myImages/"+your image file name);
ImageView mImgView = (ImageView)findViewById(R.id.imageView);
Bitmap bmp = BitmapFactory.decodeFile(f.getAbsolutePath());
mImgView.setImageBitmap(bmp);
由于此操作将数据保存在外部内存中,因此需要
AndroidManifest.xml
权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
我猜可能有很多类似的问题。