1
我面臨的問題我無法解決一個星期。所以,我試圖做的是將圖像保存到內部存儲器中,取回它的URI並將其保存到SQLite數據庫。我的問題是,我無法從匿名的方式獲得URI稱爲方法「完成」。我無法重寫它,因爲我使用Parse API將ParseFile轉換爲Image。代碼如下:Android - 無法從匿名調用的方法獲取URI
public class DataFetch extends IntentService {
public final String LOG_TAG = DataFetch.class.getSimpleName();
private TestUri mImageUri;
public DataFetch() {
super("UriTest");
}
@Override
protected void onHandleIntent(Intent intent) {
//Set up background task here
try {
// Locate the class table named "Test_Images" in Parse.com
ParseQuery<ParseObject> queryTest = new ParseQuery<ParseObject>(
"Test_Images");
List<ParseObject> testData = queryTest.find();
//Insert the new test information into the database
Vector<ContentValues> cVVector = new Vector<ContentValues>(testData.size());
for(ParseObject object : testData) {
// Create a ContentValues object to hold data which would be added to DB.
final ContentValues testDataValues = new ContentValues();
ParseFile image = (ParseFile)object.get("image");
final String imageName = object.getString("name");
//Log.i("data_fetch","before inner class: "+mImageUri.getUri());
if(image != null){
mImageUri = new TestUri();
image.getDataInBackground(new GetDataCallback() {
public void done(byte[] data, ParseException e) {
if (e == null) {
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
Log.d(LOG_TAG,"Bitmap straight after creation: "+bitmap);
if(bitmap != null){
//Set image of the event in place
mImageUri.setUri(saveImageToInternalStorage(bitmap, imageName));
Log.i("data_fetch", "inner class: " + mImageUri.getUri());
Log.d(LOG_TAG, "Bitmap straight after processing: " + bitmap + " and its length is " + data.length);
}
else {
Log.d(LOG_TAG, "file null?");
mImageUri.setUri("nothing_here");
}
}
else
Log.d(LOG_TAG, "ParseFile ParseException: " + e.toString());
}
});
}
else {
Log.d(LOG_TAG, "ParseFile is null");
}
Log.i("data_fetch","after inner class: "+mImageUri.getUri());
// Insert the name and id of the data.
testDataValues.put(DataTestContract.TestFieldEntry.COL_NAME, imageName);
Log.i("data_fetch", "name: " + object.getString("name"));
testDataValues.put(DataTestContract.TestFieldEntry.COL_CAT, object.getString("Id"));
Log.i("data_fetch", "id: " + object.getString("Id"));
// Insert image URI into ContentValues object.
testDataValues.put(DataTestContract.TestFieldEntry.COL_URI, mImageUri.getUri());
Log.i("data_fetch","image uri: "+mImageUri.getUri());
cVVector.add(testDataValues);
}
//Add test data to database
int inserted = 0;
int deleted = 0;
// add to database
if (cVVector.size() > 0) {
ContentValues[] cvArray = new ContentValues[cVVector.size()];
cVVector.toArray(cvArray);
for(int i=0;i<cvArray.length;++i){
Log.i("data_fetch","data passed to cvArray: "+cVVector.get(i));
Log.i("data_fetch", "data to be inserted: " + cvArray[i]);
}
getApplicationContext().getContentResolver().bulkInsert(DataTestContract.TestFieldEntry.CONTENT_URI, cvArray);
//Delete old data from DB
}
Log.d(LOG_TAG, "Sync Complete. " + cVVector.size() + " Inserted. Deleted:"+deleted);
} catch (ParseException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
}
private String saveImageToInternalStorage(Bitmap image, String name) {
ContextWrapper cw = new ContextWrapper(getApplicationContext());
// path to /data/data/yourapp/app_data/imageDir
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
// Create imageDir
name.trim();
File mypath=new File(directory, name+".jpg");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mypath);
// Use the compress method on the BitMap object to write image to the OutputStream
image.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
return mypath.getAbsolutePath();
}
public static class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent sendIntent = new Intent(context, DataFetch.class);
context.startService(sendIntent);
}
}
}
感謝提前任何幫助!
up up up up up up up up! – JDRussia
up up up up up up up! – JDRussia