[英]QR code Image not saved from bitmap in my android app
I create a QR Code Generator and there is a "save" button.我创建了一个二维码生成器,并有一个“保存”按钮。 so I want when I press the save button it should be saved on phone storage.所以我想当我按下保存按钮时,它应该保存在手机存储中。 but when I run this code the QR code generates well but not saves it its shows me in the Toast "Image Not Saved".但是当我运行此代码时,QR 代码生成良好但未保存它在 Toast 中显示“未保存图像”。 how can I save this problem?我怎样才能解决这个问题? I want to save the QR code as an image in the phone storage.我想将二维码保存为手机存储中的图像。 then people can save it and use it.然后人们可以保存它并使用它。 I hope you guys will help me to solve this problem.我希望你们能帮我解决这个问题。 thanks in advance.提前致谢。
EditText editText;
ImageView imageView;
Button btnGenerate, save;
Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qrgenerator);
editText = findViewById(R.id.editText);
imageView = findViewById(R.id.imageView);
btnGenerate = findViewById(R.id.generate);
save = findViewById(R.id.save);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
btnGenerate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String data = editText.getText().toString();
if (data.isEmpty()) {
editText.setError("Value Required");
} else {
QRGEncoder qrgEncoder;
qrgEncoder = new QRGEncoder(data, null, QRGContents.Type.TEXT, 500);
try {
bitmap = qrgEncoder.encodeAsBitmap();
imageView.setImageBitmap(bitmap);
} catch (WriterException e) {
e.printStackTrace();
}
}
}
});
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
boolean save;
String result;
String savePath = Environment.getExternalStorageDirectory().getPath() + "/QRCode/";
try {
save = QRGSaver.save(savePath, editText.getText().toString().trim(), bitmap, QRGContents.ImageType.IMAGE_PNG);
result = save ? "Image Saved" : "Image Not Saved";
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (item.getItemId()== android.R.id.home){
this.finish();
}
return super.onOptionsItemSelected(item);
}
}
i also add this permission in the Manifest file.我还在清单文件中添加了这个权限。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Try change尝试改变
Environment.getExternalStorageDirectory().getPath() + "/QRCode/"
to到
Environment.getExternalStoragePublicDirectory().getPath() + "/QRCode/"
and dont forget to check user permission并且不要忘记检查用户权限
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.