[英]Compress image in android (b4a)
I'm using this java code to compress an image in basic4android ! 我正在使用此Java代码在basic4android中压缩图像! However, nothing works!
但是,没有任何效果!
#if java
import android.graphics.Bitmap;
import java.io.*;
import android.os.Environment;
public void Resize(Bitmap mPhoto)
{
try
{
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
mPhoto.compress(Bitmap.CompressFormat.PNG, 50, bytes);
File file = new File(Environment.getExternalStorageDirectory()+"/"+"11111.jpg");
file.createNewFile();
FileOutputStream fo = new FileOutputStream(file);
fo.write(bytes.toByteArray());
fo.close();
}
catch (Exception e) {}
}
#end if
No file is shown under ExternalStorageDirectory. 在ExternalStorageDirectory下没有文件显示。
Try first to create a directory in the root directory and then write the file in it like this for example: 首先尝试在根目录中创建一个目录,然后像这样将文件写入其中:
File sdcard = Environment.getExternalStorageDirectory();
File dir = new File(sdcard.getAbsolutePath() + “/your-dir-name/”);
dir.mkdir();
File file = new File(dir, “11111.jpg”);
FileOutputStream os = outStream = new FileOutputStream(file);
os.write(bytes.toByteArray());
os.close();
And please do not forget to add this permission to your AndroidManifest-file 并且请不要忘记将此权限添加到您的AndroidManifest文件中
<uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE” />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.