我有一个拍照的应用程序。由于不再能够在意图中传递URI,我最近不得不更改部分应用程序。所以我将部分代码从
file_uri = Uri.fromFile(file);
改为了
file_uri = FileProvider.getUriForFile(PhotoActivity.this,BuildConfig.APPLICATION_ID + ".provider", file);
但是现在这似乎打破了我的代码的另一部分,我无法弄清楚如何解决它。
这是相关部分:
private class Encode_image extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
bitmap = BitmapFactory.decodeFile(file_uri.getPath());
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
bitmap.recycle();
byte[] array = stream.toByteArray();
encoded_string = Base64.encodeToString(array, 0);
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
makeRequest();
}
}
private void makeRequest() {
RequestQueue requestQueue = Volley.newRequestQueue(this);
StringRequest request = new StringRequest(Request.Method.POST, POST_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String,String> map = new HashMap<>();
map.put("encoded_string",encoded_string);
map.put("image_name",image_name);
return map;
}
};
requestQueue.add(request);
}
private void getFileUri() {
String uuid = UUID.randomUUID().toString();
imageName=uuid;
image_name = uuid + ".jpg";
file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
+ File.separator + image_name
);
//file_uri = Uri.fromFile(file);
file_uri = FileProvider.getUriForFile(PhotoActivity.this,
BuildConfig.APPLICATION_ID + ".provider",
file);
}
这是我得到的错误:
E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /external_files/Pictures/aba094a2-3f15-405f-9203-fdc4245dc003.jpg (No such file or directory)
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: com.example.gabrieluliano.my_sick_app, PID: 8639
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:318)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:762)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference
at com.example.gabrieluliano.my_sick_app.PhotoActivity$Encode_image.doInBackground(PhotoActivity.java:347)
at com.example.gabrieluliano.my_sick_app.PhotoActivity$Encode_image.doInBackground(PhotoActivity.java:331)
at android.os.AsyncTask$2.call(AsyncTask.java:304)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:762)
任何人都知道它有什么问题吗?感谢
编辑: 我将位图部分更改为:
try(InputStream is = new URL( file_uri.getPath() ).openStream()){
bitmap = BitmapFactory.decodeStream(is);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
bitmap.recycle();
byte[] array = stream.toByteArray();
encoded_string = Base64.encodeToString(array, 0);
return null;
}
catch(Exception e){
return null;
}
答案 0 :(得分:0)
尝试使用BitmapFactory.decodeStream
而不是BitmapFactory.decodeFile
。
Here是如何使用它的一个示例。
答案 1 :(得分:0)
create table practice_table
AS SELECT table1.column1, table2.column2, count(table3.column3) ColumnAlias
FROM practice1 table1
JOIN practice2 table2 ON table1.table1ID = table2.table2ID
JOIN practice3 table3 ON table1.table1ID = table3.table3ID
where table2.column2 in ('filter1','filter2')
GROUP BY table1.column1, t2.column2;
更改为
bitmap = BitmapFactory.decodeFile(file_uri.getPath());
但是当然首先从文件中创建一个Bitmap并不好。
您当然应该“按原样”上传文件,而不要乱用Bitmaps和BitmapFactory。