简体   繁体   中英

Android save image to external storage

I have a camera application. It should take picture and then store it to external storage but I got following log error. Please help me. Why I am getting this error?

Here is the logcat:

在此输入图像描述

Here is the code of store in storage:

Bitmap bitmap = BitmapFactory.decodeByteArray(Constant.imageData,
                0, Constant.imageData.length);

        ByteArrayOutputStream stream = new ByteArrayOutputStream();

        bitmap.compress(Bitmap.CompressFormat.JPEG, 50, stream);

        File file = new File(Environment.getExternalStorageDirectory().toString()
                + File.separator + "Android" + File.separator + "data"
                + File.separator + context.getPackageName()
                + File.separator + "files" + File.separator + "image.jpg");

        Log.e("pa", file.getPath() + "  :  " + file.getAbsolutePath());
        Log.e("len", stream.size() + "");

        file.createNewFile();

        /*
         * Log.e("Camrera", "yesssssssssssssssssss"); Log.e("Camrera",
         * pictureFile.getAbsolutePath());
         */
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(stream.toByteArray());
        fos.close();

I have answered this question link is below:

Android: Get a file from http and store in SDCard

Modify the code according to your requirement

Below is the code:

private void savePrivateExternalFile(String fileURL, String fName) {
   HttpURLConnection connection = null;
  URL url = null;
  try {

    url = new URL(fileURL);
    connection = (HttpURLConnection) url.openConnection();
    connection.addRequestProperty(BConstant.WEB_SERVICES_COOKIES,
            cookie);
         connection.setDoOutput(true);
    connection.connect();
} catch (IOException e1) {
    e1.printStackTrace();
}
File folderDir = null;
    folderDir = new File(getExternalFilesDir("Directory Name") + "/Files");

File file = new File(folderDir, fName);

if (file.exists()) {
    file.delete();
}

if ((folderDir.mkdirs() || folderDir.isDirectory())) {
    try {
        InputStream inputStream = connection.getInputStream();
        BufferedInputStream bufferedInputStream = null;

            bufferedInputStream = new BufferedInputStream(inputStream,
                    1024 * 5);

        FileOutputStream fileOutputStream = new FileOutputStream(
                folderDir + "/" + fName);
        byte[] buffer = new byte[1024];
            int len1 = 0;
            while ((len1 = inputStream.read(buffer)) != -1) {
                fileOutputStream.write(buffer, 0, len1);
            }

            bufferedInputStream.close();

        fileOutputStream.close();
        inputStream.close();
        connection.disconnect();
        } catch (Exception e) {
        e.printStackTrace();
    }
  }

Use this if you want to open Downloaded file :

   File file = new File(getExternalFilesDir("Directory Name")+ "/Files/" + fileName);
    Intent intent = new Intent(Intent.ACTION_VIEW);
  intent.setDataAndType(Uri.fromFile(file),"application/pdf");
  intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
  startActivity(intent);

Add this line in your Manifest file:

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

This code explains a lot about how to store the images to sdcard and add the images to MediaStore.

File imagesFolder;
ByteArrayOutputStream bytes = new ByteArrayOutputStream();

if(BitmapImage != null)
{ 


BitmapImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);

}
if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
{
   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) 
        {
        imagesFolder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"/MyAppName");
            }
            else
        {
        imagesFolder=   new File (Environment.getExternalStorageDirectory() + "/dcim/"+"MyAppName");
        }
            if (!imagesFolder.exists())
            {
                imagesFolder.mkdirs();
            }



            try 
            {
                myfile = File.createTempFile("Image_Name", ".jpeg", imagesFolder);
                FileOutputStream out = new FileOutputStream(myfile);


                out.write(bytes.toByteArray());
                out.flush();
                out.close();

            }
            catch (IOException e1) 
            {
                e1.printStackTrace();
            }
       Intent mediaScanIntent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE");

            Uri   contentUri = Uri.fromFile(myfile);
               mediaScanIntent.setData(contentUri);
               this.sendBroadcast(mediaScanIntent);

你使用这个许可吗?

uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM