简体   繁体   English

在Android Studio中将图片保存到图库中时出错

[英]Error to save image in gallery in Android Studio

public class MainActivity extends Activity {

    ImageButton b1, b2;
    ImageView v1;
    private static int RESULT_LOAD_IMAGE = 1;
    BitmapDrawable drawable;
    Bitmap bitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        b1 = findViewById(R.id.card);
        b2 = findViewById(R.id.save);
        v1 = findViewById(R.id.imageview1);

        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(Intent.ACTION_PICK);
                i.setType("image/*");
                startActivityForResult(i, RESULT_LOAD_IMAGE);
            }
        });

        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                drawable = (BitmapDrawable) v1.getDrawable();
                bitmap = drawable.getBitmap();

                FileOutputStream outputStream = null;
                File sdCard = Environment.getExternalStorageDirectory();
                File directory = new File(sdCard.getAbsolutePath() + "YourFolderName");
                directory.mkdir();


                @SuppressLint("DefaultLocale") String filename = String.format("%d.jpg",System.currentTimeMillis());
                File outFile = new File(directory,filename);
                try {
                    outputStream = new FileOutputStream(outFile);
                    bitmap.compress(Bitmap.CompressFormat.JPEG,100,outputStream);
                    outputStream.flush();
                    outputStream.close();

                    Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                    intent.setData(Uri.fromFile(outFile));
                    sendBroadcast(intent);

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }


            }
        });

    }


    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};

            ImageView imageView = findViewById(R.id.imageview1);
            try {
                Bitmap bm = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedImage);
                imageView.setImageBitmap(bm);
            } catch (FileNotFoundException e) {
            } catch (IOException e) {
            }
        }
    }

    public static void saveFrameLayout(FrameLayout frameLayout, String path) {
        frameLayout.setDrawingCacheEnabled(true);
        frameLayout.buildDrawingCache();
        Bitmap cache = frameLayout.getDrawingCache();
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(path);
            cache.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (Exception e) {
            // TODO: handle exception
        } finally {
            frameLayout.destroyDrawingCache();
        }
    }

}

I want to save the image in the gallery. 我想将图像保存在图库中。 It is running but it is showing error on the run time. 它正在运行,但是在运行时显示错误。 Here I am picking the Image from the gallery and want to save that image again in the gallery. 在这里,我从图库中选择图像,然后再次将该图像保存在图库中。 Already given the different path to save that picked image. 已经指定了保存该拾取图像的不同路径。

It is showing 它显示

"W/System.err: at >android.app.ActivityThread.main(ActivityThread.java:6692)" while I run the >code. 我运行代码时,“ W / System.err:位于> android.app.ActivityThread.main(ActivityThread.java:6692)”。 I already done entry in manifest file. 我已经在清单文件中输入了内容。

Your file (or the directory to said file) doesn't exist, you'll need to create the file using createNewFile , something along the following lines should work, 您的文件(或指向该文件的目录)不存在,您需要使用createNewFile创建文件,以下几行应该可以工作,

File outFile = new File(directory,filename);

//The below line shouldn't be required since you do directory.mkdir()
//file.getParentFile().mkdirs(); // Will create parent directories if not exists

file.createNewFile(); // Will create file if it doesn't exist

try {
    //Rest of your code

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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