如何找出应用程序保存我的可变位图的位置

问题描述 投票:0回答:1

我正在研究一个模因发生器。现在,我正在尝试从我的设备上传照片,通过Canvas在照片上添加标题,并通过在其中创建一个新文件夹将新位图保存到我设备的“图库”应用程序中。这是我的尝试:

public void createBitmapAndSave(ImageView img){
        BitmapDrawable bitmapDrawable = ((BitmapDrawable) img.getDrawable());
        Bitmap bitmap = bitmapDrawable.getBitmap();
        Bitmap mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);

        Canvas canvas = new Canvas(mutableBitmap);
        Paint paint = new Paint();

        String topText = topTextView.getText().toString();
        String bottomText = bottomTextView.getText().toString();

        canvas.drawText(topText, 0, 0, paint);
        canvas.drawText(bottomText, 50, 50, paint);

        File file;

        String path = Environment.getExternalStorageDirectory().toString();

        file = new File(path, "Meme" + ".jpg");

        try{
            OutputStream stream = null;

            stream = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG,100,stream);
            stream.flush();
            stream.close();
        }catch (IOException e){ e.printStackTrace(); }

    }

我在我的BroadcastReceiver中调用方法:

private BroadcastReceiver listener = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent ) {
            String data = intent.getStringExtra("DATA");
            Toast.makeText(context, data + " received", Toast.LENGTH_SHORT).show();
            createBitmapAndSave(imageView);
        }
    };

目前,我甚至不确定位图的编辑或保存是否有效,因为我在应用程序的文件夹中找不到它。

UPDATE

我发现了这个问题。我决定在try / catch块中放置一个toast消息,看看该流是否正常工作,因为它在try / catch块之外,我会在每次运行时看到它。这就是我做的:

public void createBitmapAndSave(ImageView img){
        BitmapDrawable bitmapDrawable = ((BitmapDrawable) img.getDrawable());
        Bitmap bitmap = bitmapDrawable.getBitmap();
        Bitmap mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);

        Canvas canvas = new Canvas(mutableBitmap);
        Paint paint = new Paint();

        String topText = topTextView.getText().toString();
        String bottomText = bottomTextView.getText().toString();

        canvas.drawText(topText, 0, 0, paint);
        canvas.drawText(bottomText, 50, 50, paint);

        File file;

        String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath();
        file = new File(path, "Meme" + ".jpg");

        try{
            OutputStream stream = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG,100,stream);
            stream.flush();
            stream.close();
            Toast.makeText(getContext(), path, Toast.LENGTH_SHORT).show();
        }catch (IOException e){ e.printStackTrace();}
    }

现在,我根本没有看到吐司的消息。看起来流甚至没有正常工作,但我不明白为什么它不会。

java android android-canvas android-bitmap
1个回答
1
投票

不要使用String path = Environment.getExternalStorageDirectory().toString();它会将您的文件存储在用户看不到的私有存储中。

使用File path = Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES);

© www.soinside.com 2019 - 2024. All rights reserved.