我的應用程序的功能:LibGDX:如何檢索通過相機拍攝並保存在本地的圖片?
1-用戶可以通過設備相機拍照。 (作品)
2-應用程序創建下列文件夾一個新的文件(以下文件夾測試,以確保其正確地保存文件):
a. File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "images");
- File mediaFile = new File(mediaStorageDir.getPath()+File.separator+fileName + ".jpg");
b. File mediaStorageDir = new File(Gdx.files.getExternalStoragePath().toString());
- File mediaFile = new File(mediaStorageDir.getPath()+File.separator+fileName+".jpg");
c. File mediaStorageDir = new File(Gdx.files.getLocalStoragePath().toString());
- File mediaFile = new File(mediaStorageDir.getPath()+File.separator+fileName+".jpg");
3 - 應用程序resmples +壓縮圖片降至1024x512與bmp.compress(質量= 25),然後保存它。 (作品)
public boolean compressToFile(byte[] data, int quality, File fileHandle) {
File mediaFile = fileHandle;
Pixmap pixmap = new Pixmap(data, 0, data.length);
if(quality<0)
quality = 0;
if(quality>100)
quality = 100;
FileOutputStream fos;
int x=0,y=0;
int xl=0,yl=0;
try {
Bitmap bmp = Bitmap.createBitmap(pixmap.getWidth(), pixmap.getHeight(), Bitmap.Config.ARGB_8888);
// we need to switch between LibGDX RGBA format to Android ARGB format
for (x=0,xl=pixmap.getWidth(); x<xl;x++) {
for (y=0,yl=pixmap.getHeight(); y<yl;y++) {
int color = pixmap.getPixel(x, y);
// RGBA => ARGB
int RGB = color >> 8;
int A = (color & 0x000000ff) << 24;
int ARGB = A | RGB;
bmp.setPixel(x, y, ARGB);
}
}
fos = new FileOutputStream(mediaFile, false);
boolean compressed = bmp.compress(CompressFormat.JPEG, quality, fos);
if(compressed)
System.out.println("zgzg2020:: compressed SUCCESS!");
else
System.out.println("zgzg2020:: compressed FAILED!");
fos.close();
int WIDTH = 1024, HEIGHT = 512;
File f = mediaFile;
Bitmap shrunkBmp = downsizeImage(f, WIDTH, HEIGHT);
fos = new FileOutputStream(mediaFile, false);
shrunkBmp.compress(CompressFormat.JPEG, 100, fos);
fos.close();
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
return false;
}
比例縮小。
public Bitmap downsizeImage(File file, int width, int height) {
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = 2; // for 1/2 the image to be loaded
Bitmap thumb = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(file.getPath(), opts), width, height, false);
return thumb;
}
4-確認jpg保存正確。我已經用上述三種路徑進行了測試。
System.out.println("b4 pictureFile= " + file.getPath().toString() + "=> " + file.exists());//Returns false.
compressToFile(data, quality, file);//Here is where the compression, scale down, write to disk.
System.out.println("af pictureFile= " + file.getPath().toString() + "=> " + file.exists());//Returns true
5-讀取保存的圖片並將其顯示在屏幕上。 應用程序在這裏崩潰!!!
mode = Mode.render;
System.out.println("AssessPath:"+file.toString());//to confirm the path
texture = new Texture(file.toString());//(FAILS!!)
錯誤
com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load file
注:
我測試過我的應用程序在兩臺機器上,棉花糖,棒棒糖。兩者都有相同的錯誤失敗。
權限似乎正確:
節省了機器上的文件後,我手動將文件複製到該文件夾中的資產。該應用程序能夠打開該文件;所以我確信該文件符合LibGDX分辨率...等要求。
我經歷了那麼多的文章和問題/答案搜索,找不到我的問題的原因:
https://github.com/libgdx/libgdx/wiki/File-handling#writing-to-a-file
https://github.com/libgdx/libgdx/wiki/Integrating-libgdx-and-the-device-camera
com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load file: - Error
謝謝你的快速回復!我會馬上測試它,讓你知道它是怎麼回事! – coffeenet
我在上面添加了我的代碼。然而,galleryOpener.openGallery()會在我的設備上打開圖庫,但應用程序的流程會直接繼續到下面的代碼,而無需等待我選擇照片;從而崩潰了「batch.beging()texture = null」。如何強制openGallery方法等待我選擇照片,然後再將控件返回給字符串selectedImagePath = galleryOpener.getSelectedImagePath();線? – coffeenet
我仍然卡住...控件只是不等待用戶在返回到渲染循環之前在galleryOpener中選擇圖片。它只是繼續與空指針崩潰。恐怕這個答案不起作用。 – coffeenet