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

标签 java android android-canvas android-bitmap

我正在开发模因生成器。现在,我正在尝试从我的设备上传照片,通过 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);
        }
    };

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

更新

我发现了这个问题。我决定在 try/catch block 内放置一条 toast 消息,以查看流是否正常工作,因为之前它位于 try/catch block 之外,并且每次运行时我都会看到它。这就是我所做的:

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();}
    }

现在,我根本看不到 Toast 消息。似乎该流甚至无法正常工作,但我不明白为什么它不能正常工作。

最佳答案

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

使用文件路径=Environment.getExternalStoragePublicDirectory( 环境.DIRECTORY_PICTURES);

关于java - 如何找出应用程序保存我的可变位图的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54822979/

相关文章:

java - 为什么eclipse在java的outline View 中隐藏private成员?

java - 是什么导致了 java.lang.ArrayIndexOutOfBoundsException 以及如何防止它?

android - 在 Android 中设计一个按钮

Android 从 Canvas 中获取位图

java - Spring Reactor 线程模型

java - NetworkInterface.getNetworkInterfaces() 没有列出所有接口(interface)

android - 在 webview 中使用 javascript 加载 html

android - 如何在android中在中心画一个圆

android.graphics.Picture 未在 API 14+ 中绘制