生成二维码后如何保存名称

s6fujrry  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(406)

我想做一个应用程序,生成二维码。它运行良好,但保存时没有文本。因为上面没有名字,人们在保存了一些代码之后会感到困惑。
如果人们使用“wikipedia.com”生成二维码,我希望它在照片库中以“wikipedia.com”的名称保存。我该怎么办?
主要活动

public class MainActivity extends AppCompatActivity {

    private String inputValue;
    private String savePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + "/Camera/";
    private Bitmap bitmap;
    private QRGEncoder qrgEncoder;
    private ImageView qrImage;
    private EditText edtValue;
    private AppCompatActivity activity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        qrImage = findViewById(R.id.qr_image);
        edtValue = findViewById(R.id.edt_value);
        activity = this;

/**Barcode Generator*/
        findViewById(R.id.generate_barcode).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                inputValue = edtValue.getText().toString().trim();
                if (inputValue.length() > 0) {
                    WindowManager manager = (WindowManager) getSystemService(WINDOW_SERVICE);
                    Display display = manager.getDefaultDisplay();
                    Point point = new Point();
                    display.getSize(point);
                    int width = point.x;
                    int height = point.y;
                    int smallerDimension = width < height ? width : height;
                    smallerDimension = smallerDimension * 3 / 4;

                    qrgEncoder = new QRGEncoder(
                            inputValue, null,
                            QRGContents.Type.TEXT,
                            smallerDimension);
                    qrgEncoder.setColorBlack(Color.BLACK);
                    qrgEncoder.setColorWhite(Color.WHITE);
                    try {
                        bitmap = qrgEncoder.getBitmap();
                        qrImage.setImageBitmap(bitmap);
                    } catch (Exception e) {
                        e.printStackTrace();

                    }
                } else {
                    edtValue.setError(getResources().getString(R.string.value_required));
                }
            }

        });
/**Barcode save*/
        findViewById(R.id.save_barcode).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
                    try {
                        boolean save = new QRGSaver().save(savePath, edtValue.getText().toString().trim(), bitmap, QRGContents.ImageType.IMAGE_JPEG);
                        String result = save ? "Image Saved. Check your gallery." : "Image Not Saved";
                        Toast.makeText(activity, result, Toast.LENGTH_LONG).show();
                        edtValue.setText(null);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
                }
            }
        });
    }
}
wh6knrhe

wh6knrhe1#

我试过你的代码,效果很好。生成的二维码将按您的需要保存:qr-code-text.jpg
唯一的问题是qrgsaver().save(…)与android10+不兼容。
尝试按如下方式扩展保存条形码按钮的onclicklistener:

/*Barcode save*/
findViewById(R.id.save_barcode).setOnClickListener(v -> {
    String filename = edtValue.getText().toString().trim();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        try {
            ContentResolver resolver = getContentResolver();
            ContentValues contentValues = new ContentValues();
            contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, filename + ".jpg");
            contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpg");
            contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DCIM);
            Uri imageUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
            OutputStream fos = resolver.openOutputStream(Objects.requireNonNull(imageUri));
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            Objects.requireNonNull(fos).close();
            Toast.makeText(activity, "Image Saved. Check your gallery.", Toast.LENGTH_LONG).show();
            edtValue.setText(null);
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
            try {
                boolean save = new QRGSaver().save(savePath, filename, bitmap, QRGContents.ImageType.IMAGE_JPEG);
                String result = save ? "Image Saved. Check your gallery." : "Image Not Saved";
                Toast.makeText(activity, result, Toast.LENGTH_LONG).show();
                edtValue.setText(null);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
        }
    }
});

相关问题