store an image in internal storage in android

為{幸葍}努か 提交于 2019-11-30 16:18:50

问题


I am working now on application that start the camera and take a photo, I didn't use camera activity, but I wrote my camera app and I want to save the taken image in the internal phone storage in folder called "temPic"

The following code generate the folder and the image, but when I checked the tempPic I found an image called image1.jpg and it's size is 461115 ( I tried to store the image in SDcard directory and it is the same size), but when I double clicked it a black image appeared, not the taken one although in SDcard I opened it !!!

FileManipulator fileFormat = new FileManipulator(
                getApplicationContext());
        String path = fileFormat.createFolder_PStrg("tempPic") + "/image1.jpg";     
        File file = new File(path);
        Uri outputFileUri = Uri.fromFile(file);

        OutputStream imageFileOS;
        try {


            imageFileOS = getContentResolver().openOutputStream(outputFileUri);
            imageFileOS.write(arg0);
            imageFileOS.flush();
            imageFileOS.close();
            Toast.makeText(AndroidCamera.this, 
                    file.length()+"", 
                    Toast.LENGTH_LONG).show();
            Toast.makeText(AndroidCamera.this, 
                    "Image saved: " + outputFileUri.toString(), 
                    Toast.LENGTH_LONG).show();

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        camera.startPreview();

回答1:


I don't know if this can help you but this is how I save a file to the SD card and it works.

        public void saveToSD(Bitmap outputImage){


            File storagePath = new File(Environment.getExternalStorageDirectory() + "/MyPhotos/"); 
            storagePath.mkdirs(); 

            File myImage = new File(storagePath, Long.toString(System.currentTimeMillis()) + ".jpg");

            try { 
                FileOutputStream out = new FileOutputStream(myImage); 
                outputImage.compress(Bitmap.CompressFormat.JPEG, 80, out); 
                out.flush();    
                out.close();
            } catch (Exception e) { 
                e.printStackTrace(); 
            }               
        }



回答2:


Why don't you use the camera activity and save images to internal storage by creating temp folder that has writable permission which allow other application to write to this folder and take the image to its path and then after the camera activity return you can move the image to another internal storage folder but with private permission as follow



//the temp folder to start the camera activity with
String path = getDir("images", Context.MODE_WORLD_WRITEABLE).getPath() + "/test.jpg";

//start the camera activity
File file = new File(path);
Uri outputFileUri = Uri.fromFile(file);

Intent intent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, CAMERA_ACTIVITY);

and in onActivityResult method move it to folder with private permission it will work fine as I tested it



来源:https://stackoverflow.com/questions/5766579/store-an-image-in-internal-storage-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!