我可以在Imageview中加载图库和显示图像。我需要将用户在parse.com中选择的图像保存为黑化。它是一个我可以使用BitmapFactory加载的picturepath。我在数据浏览器中创建了一个列,其中字段为File。我还浏览了Parse.com上的教程并实现了每一步。但单击保存按钮时它无法正常工作。应用程序每次都崩溃。
我的代码是:
public class LoadImg extends Activity {
private static int RESULT_LOAD_IMAGE = 1;
FrameLayout layout;
Button save ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
save=(Button) findViewById(R.id.button2);
String picturePath = PreferenceManager.getDefaultSharedPreferences(this).getString("picturePath", "");
if(!picturePath.equals(""))
{
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
Button buttonLoadImage = (Button) findViewById(R.id.button1);
buttonLoadImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// Locate the image in res >
Bitmap bitmap = BitmapFactory.decodeFile("picturePath");
// Convert it to byte
ByteArrayOutputStream stream = new ByteArrayOutputStream();
// Compress image to lower quality scale 1 - 100
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] image = stream.toByteArray();
// Create the ParseFile
ParseFile file = new ParseFile("picturePath", image);
// Upload the image into Parse Cloud
file.saveInBackground();
// Create a New Class called "ImageUpload" in Parse
ParseObject imgupload = new ParseObject("ImageUpload");
// Create a column named "ImageName" and set the string
imgupload.put("Image", "picturePath");
// Create a column named "ImageFile" and insert the image
imgupload.put("ImageFile", file);
// Create the class and the columns
imgupload.saveInBackground();
// Show a simple toast message
Toast.makeText(LoadImg.this, "Image Uploaded",Toast.LENGTH_SHORT).show();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
PreferenceManager.getDefaultSharedPreferences(this).edit().putString("picturePath", picturePath).commit();
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}}
LogCat是:
01-05 20:43:54.678:E / JavaBinder(1298):!!!失败的粘合剂交易! 01-05 20:44:48.418:E / AndroidRuntime(3453):未捕获的处理程序:由于未捕获的异常而导致线程主要退出 01-05 20:44:48.428:E / AndroidRuntime(3453):java.lang.NullPointerException 01-05 20:44:48.428:E / AndroidRuntime(3453):at com.lovy.parse.LoadImg $ 2.onClick(LoadImg.java:56) 01-05 20:44:48.428:E / AndroidRuntime(3453):在android.view.View.performClick(View.java:2418) 01-05 20:44:48.428:E / AndroidRuntime(3453):在android.view.View.onTouchEvent(View.java:4233) 01-05 20:44:48.428:E / AndroidRuntime(3453):在android.widget.TextView.onTouchEvent(TextView.java:6645) 01-05 20:44:48.428:E / AndroidRuntime(3453):在android.view.View.dispatchTouchEvent(View.java:3763) 01-05 20:44:48.428:E / AndroidRuntime(3453):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:968) 01-05 20:44:48.428:E / AndroidRuntime(3453):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:968) 01-05 20:44:48.428:E / AndroidRuntime(3453):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:968) 01-05 20:44:48.428:E / AndroidRuntime(3453):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:968) 01-05 20:44:48.428:E / AndroidRuntime(3453):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:968) 01-05 20:44:48.428:E / AndroidRuntime(3453):at com.android.internal.policy.impl.PhoneWindow $ DecorView.superDispatchTouchEvent(PhoneWindow.java:1659) 01-05 20:44:48.428:E / AndroidRuntime(3453):at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107) 01-05 20:44:48.428:E / AndroidRuntime(3453):在android.app.Activity.dispatchTouchEvent(Activity.java:2064) 01-05 20:44:48.428:E / AndroidRuntime(3453):at com.android.internal.policy.impl.PhoneWindow $ DecorView.dispatchTouchEvent(PhoneWindow.java:1643) 01-05 20:44:48.428:E / AndroidRuntime(3453):在android.view.ViewRoot.handleMessage(ViewRoot.java:1691) 01-05 20:44:48.428:E / AndroidRuntime(3453):在android.os.Handler.dispatchMessage(Handler.java:99) 01-05 20:44:48.428:E / AndroidRuntime(3453):在android.os.Looper.loop(Looper.java:123) 01-05 20:44:48.428:E / AndroidRuntime(3453):在android.app.ActivityThread.main(ActivityThread.java:4370) 01-05 20:44:48.428:E / AndroidRuntime(3453):at java.lang.reflect.Method.invokeNative(Native Method) 01-05 20:44:48.428:E / AndroidRuntime(3453):at java.lang.reflect.Method.invoke(Method.java:521) 01-05 20:44:48.428:E / AndroidRuntime(3453):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868) 01-05 20:44:48.428:E / AndroidRuntime(3453):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 01-05 20:44:48.428:E / AndroidRuntime(3453):at dalvik.system.NativeStart.main(Native Method) 01-05 20:44:48.438:E / SemcCheckin(3453):获取崩溃转储级别:java.io.FileNotFoundException:/ data / semc-checkin / crashdump 01-05 20:44:48.508:E / SemcCheckin(1565):获取崩溃级别:java.io.FileNotFoundException:/ data / semc-checkin / crashdump 01-05 20:44:58.378:E / ATK(2142):widget kill start
答案 0 :(得分:5)
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// Locate the image in res >
//Bitmap bitmap = BitmapFactory.decodeFile("picturePath");
// Convert it to byte
//ByteArrayOutputStream stream = new ByteArrayOutputStream();
// Compress image to lower quality scale 1 - 100
//bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
try {
image = readInFile(path);
}
catch(Exception e) {
e.printStackTrace();
}
// Create the ParseFile
ParseFile file = new ParseFile("picturePath", image);
// Upload the image into Parse Cloud
file.saveInBackground();
// Create a New Class called "ImageUpload" in Parse
ParseObject imgupload = new ParseObject("Image");
// Create a column named "ImageName" and set the string
imgupload.put("Image", "picturePath");
// Create a column named "ImageFile" and insert the image
imgupload.put("ImageFile", file);
// Create the class and the columns
imgupload.saveInBackground();
// Show a simple toast message
Toast.makeText(LoadImg.this, "Image Saved, Upload another one ",Toast.LENGTH_SHORT).show();
}
});
}
private byte[] readInFile(String path) throws IOException {
// TODO Auto-generated method stub
byte[] data = null;
File file = new File(path);
InputStream input_stream = new BufferedInputStream(new FileInputStream(
file));
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
data = new byte[16384]; // 16K
int bytes_read;
while ((bytes_read = input_stream.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, bytes_read);
}
input_stream.close();
return buffer.toByteArray();
}
答案 1 :(得分:0)
还没有LogCat,但我怀疑:
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.id.imageView1);
导致问题,因为R.id.imageView1是View的id而不是图像资源。
您可以尝试这样做:
Bitmap bitmap = decodeFile(picturePath);
if (bitmap != null) {
// ...
}
注意:
你也有这个:
// Create a column named "ImageName" and set the string
imgupload.put("Image", R.id.imageView1);
从评论中,你的目的是保存一个字符串,但实际上你保存了id整数。