如何从 url Android Studio 下载我的位图到画廊
How can I download my bitmap to gallery from url Android Studio
我的项目中有位图,我想从 url 下载我的位图到画廊。我该怎么办?
我确实用它来创建二维码
private Bitmap stringToBitmap(String content){
try {
QRCodeWriter writer = new QRCodeWriter();
BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, 512, 512);
int width = bitMatrix.getWidth();
int height = bitMatrix.getHeight();
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
bmp.setPixel(x, y, bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE);
}
}
return bmp;
} catch (WriterException e) {
e.printStackTrace();
return null;
}
}
}
我想将位图下载到我的图库中
@Override
public void onClick(View view) {
Intent i;
switch (view.getId()) {
case R.id.qrindir:
Bitmap imageResult = stringToBitmap(texttt.getText().toString());
//I want download bitmap in there
break;
}
}
我将它用于 stringtoBitmap
olustur.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Bitmap imageResult = stringToBitmap(texttt.getText().toString());
} });
}
如果我没听错你的问题,你想将位图保存到你的内部存储器。
在这种情况下,您可以使用 Bitmap.compress()
方法,例如,
try {
FileOutputStream outstream = new FileOutputStream(path-to-save);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outstream);
outstream.close();
} catch (IOException e) {
...
}
还要确保为了将文件写入内部(主要)存储,您需要请求 WRITE_EXTERNAL_STORAGE
at runtime, this may not work for devices above Android 10 (API 29). In such case you can use SAF 创建文档。
试试这个:
private final void saveJpeg(@NonNull Bitmap bitmap, @NonNull String fileName) throws IOException {
// create a file output stream that will connect to external media store
OutputStream fos = null;
ContentResolver cr = getApplicationContext().getContentResolver();
ContentValues cv = new ContentValues();
// set meta data for your file
cv.put(MediaStore.MediaColumns.DISPLAY_NAME, fileName);
cv.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpg");
cv.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES);
// get uri for new image file
Uri imageUri = cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, cv);
// connect file output stream
fos = cr.openOutputStream(imageUri);
// compress bitmap and write to output stream
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
// close file out stream
fos.flush();
fos.close();
}
这会将您的位图压缩为 jpg 并写入画廊
此外,记得在您的 AndroidManifest.xml
中获得许可
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
我的项目中有位图,我想从 url 下载我的位图到画廊。我该怎么办?
我确实用它来创建二维码
private Bitmap stringToBitmap(String content){
try {
QRCodeWriter writer = new QRCodeWriter();
BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, 512, 512);
int width = bitMatrix.getWidth();
int height = bitMatrix.getHeight();
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
bmp.setPixel(x, y, bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE);
}
}
return bmp;
} catch (WriterException e) {
e.printStackTrace();
return null;
}
}
}
我想将位图下载到我的图库中
@Override
public void onClick(View view) {
Intent i;
switch (view.getId()) {
case R.id.qrindir:
Bitmap imageResult = stringToBitmap(texttt.getText().toString());
//I want download bitmap in there
break;
}
}
我将它用于 stringtoBitmap
olustur.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Bitmap imageResult = stringToBitmap(texttt.getText().toString());
} });
}
如果我没听错你的问题,你想将位图保存到你的内部存储器。
在这种情况下,您可以使用 Bitmap.compress()
方法,例如,
try {
FileOutputStream outstream = new FileOutputStream(path-to-save);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outstream);
outstream.close();
} catch (IOException e) {
...
}
还要确保为了将文件写入内部(主要)存储,您需要请求 WRITE_EXTERNAL_STORAGE
at runtime, this may not work for devices above Android 10 (API 29). In such case you can use SAF 创建文档。
试试这个:
private final void saveJpeg(@NonNull Bitmap bitmap, @NonNull String fileName) throws IOException {
// create a file output stream that will connect to external media store
OutputStream fos = null;
ContentResolver cr = getApplicationContext().getContentResolver();
ContentValues cv = new ContentValues();
// set meta data for your file
cv.put(MediaStore.MediaColumns.DISPLAY_NAME, fileName);
cv.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpg");
cv.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES);
// get uri for new image file
Uri imageUri = cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, cv);
// connect file output stream
fos = cr.openOutputStream(imageUri);
// compress bitmap and write to output stream
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
// close file out stream
fos.flush();
fos.close();
}
这会将您的位图压缩为 jpg 并写入画廊
此外,记得在您的 AndroidManifest.xml
中获得许可<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />