Java 如何找到应用程序保存我的可变位图的位置
我在做一个模因发生器。现在,我正在尝试从我的设备上传一张照片,通过画布在照片上添加标题,并通过在其中创建一个新文件夹将新位图保存到设备的Gallery应用程序中。这是我的尝试:Java 如何找到应用程序保存我的可变位图的位置,java,android,android-canvas,android-bitmap,Java,Android,Android Canvas,Android Bitmap,我在做一个模因发生器。现在,我正在尝试从我的设备上传一张照片,通过画布在照片上添加标题,并通过在其中创建一个新文件夹将新位图保存到设备的Gallery应用程序中。这是我的尝试: public void createBitmapAndSave(ImageView img){ BitmapDrawable bitmapDrawable = ((BitmapDrawable) img.getDrawable()); Bitmap bitmap = bitmapDrawa
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(); }
}
我正在广播接收器中调用该方法:
目前,我甚至不确定位图的编辑或保存是否有效,因为我无法在我的应用程序文件夹中找到它
更新
我发现了问题所在。我决定在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();}
}
现在,我根本看不到祝酒词。流似乎工作不正常,但我不明白为什么不正常。不要使用字符串path=Environment.getExternalStorageDirectory.toString;
它会将您的文件存储在用户不可见的私有存储中
使用文件路径=Environment.getExternalStoragePublicDirectory
Environment.DIRECTORY_图片 只需记录/打印路径,然后在手机的folder@BachVu它只是表示存储/模拟/0,但我的设备文件夹中没有模拟文件。它表示设备的根路径。例如,在某些手机中,它意味着内部存储使用MediaStore.Images.MediainsertImage静态方法插入新的image@BachVu是的,我理解这一点,但它仍然不能解决所有应用程序都可以看到GetExternalStorageDirectory的问题,而getExternalFilesDir则不能。但他应该使用getExternalStorageDirectory.path,而不是toString@TakeInfo. 我没有创建文件path=Environment.getexternalstoragepublicdirectoryenvironment.DIRECTORY\u图片;因此,我可以在DCIM文件夹中找到它,但Toast消息读取storage/emulated/0/DCIM,而DCIM文件夹中没有任何内容。您是否在menifest文件中添加了权限?@TakeInfo是的,问题仍然存在。DCIM文件夹中没有任何内容
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();}
}