如何找出应用程序保存我的可变位图的位置
How do find out where the app saved my mutable bitmap
我正在研究模因生成器。现在,我正在尝试从我的设备上传一张照片,通过 Canvas
在照片上添加标题,并通过在其中创建一个新文件夹将新位图保存到我设备的 "Gallery" 应用程序。这是我的尝试:
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 块内放置一条 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();}
}
现在,我根本看不到 toast 消息。似乎流甚至无法正常工作,但我不明白为什么不能。
不要使用 String path = Environment.getExternalStorageDirectory().toString();
它会将您的文件存储在用户不可见的私人存储中。
使用File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
我正在研究模因生成器。现在,我正在尝试从我的设备上传一张照片,通过 Canvas
在照片上添加标题,并通过在其中创建一个新文件夹将新位图保存到我设备的 "Gallery" 应用程序。这是我的尝试:
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 块内放置一条 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();}
}
现在,我根本看不到 toast 消息。似乎流甚至无法正常工作,但我不明白为什么不能。
不要使用 String path = Environment.getExternalStorageDirectory().toString();
它会将您的文件存储在用户不可见的私人存储中。
使用File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);