Hallo Sven,
könntest du dir mal meinen code ansehen? Ich bekomme vom Logcat diese Meldung: Attempt to invoke virtual method 'void com.parse.ParseImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference
hier kommt der code...
public class RecipesIdeas extends ListActivity {
protected EditText mRecipesChat;
protected Button mSendRecipesBtn;
protected Button mSendPicBtn;
protected List<ParseObject> mRecipes;
protected ParseImageView imgPic;
private static final String TAG = RecipesIdeas.class.getName();
private static int RESULT_LOAD_CAMERA_IMAGE = 2;
private static int RESULT_LOAD_GALLERY_IMAGE = 1;
private String mCurrentPhotoPath;
private File cameraImageFile;
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == RESULT_LOAD_GALLERY_IMAGE && 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]);
mCurrentPhotoPath = cursor.getString(columnIndex);
PreferenceManager.getDefaultSharedPreferences(this).edit().putString("picturePath", mCurrentPhotoPath).commit();
cursor.close();
imgPic = (ParseImageView) findViewById(R.id.img_recipes);
imgPic.setImageBitmap(BitmapFactory.decodeFile(mCurrentPhotoPath));
} else if (requestCode == RESULT_LOAD_CAMERA_IMAGE) {
mCurrentPhotoPath = cameraImageFile.getAbsolutePath();
}
File image = new File(mCurrentPhotoPath);
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(), bmOptions);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100 /*ignored for PNG*/, stream);
byte[] bitmapdata = stream.toByteArray();
imgPic.setImageBitmap(bitmap);
}
}
private File createImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File folder = new File(storageDir.getAbsolutePath() + "/PlayIOFolder");
if (!folder.exists()) {
folder.mkdir();
}
cameraImageFile = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
folder /* directory */
);
return cameraImageFile;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipes_ideas);
mCurrentPhotoPath = PreferenceManager.getDefaultSharedPreferences(this).getString("picturePath", "");
mRecipesChat = (EditText) findViewById(R.id.add_text);
imgPic = (ParseImageView) findViewById(R.id.img_recipes);
imgPic.setImageBitmap(BitmapFactory.decodeFile(mCurrentPhotoPath));
View.OnClickListener chooseImageListener = null;
imgPic.setOnClickListener(chooseImageListener);
mSendPicBtn = (Button) findViewById(R.id.btn_pic);
View.OnClickListener uploadListener = null;
mSendPicBtn.setOnClickListener(uploadListener);
mSendRecipesBtn = (Button) findViewById(R.id.btn_comment);
mSendRecipesBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
chooseImageListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
dialogChooseFrom();
}
};
uploadListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
byte[] image = null;
try {
image = readInFile(mCurrentPhotoPath);
} catch (Exception e) {
e.printStackTrace();
}
ParseUser currentUser = ParseUser.getCurrentUser();
String currentUserUsername = currentUser.getUsername();
final String commentStatus = mRecipesChat.getText().toString();
// Create the Parse File
final ParseFile file = new ParseFile("Comment.png", image);
// Upload the image into Parse Cloud
file.saveInBackground();
ParseObject commentObject = new ParseObject("Comment");
commentObject.put("commentStatus", commentStatus);
commentObject.put("user", currentUserUsername);
commentObject.put("Image", "Comment.png");
commentObject.put("CommentFile", file);
commentObject.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
Toast.makeText(getBaseContext(), "Your Pic is Saved!", Toast.LENGTH_LONG).show();
final ParseUser currentUser = ParseUser.getCurrentUser();
final String currentUserUsername = currentUser.getUsername();
if (currentUser == null) {
Toast.makeText(RecipesIdeas.this, "Send!", Toast.LENGTH_LONG).show();
mRecipesChat.setText("");
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Comment");
query.orderByDescending("createdAt");
query.whereEqualTo("commentStatus", commentStatus);
query.whereEqualTo("user", currentUserUsername);
query.whereEqualTo("objectId", currentUserUsername);
Log.v(TAG, "user" + currentUserUsername);
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> imgProfile, ParseException e) {
if (e == null) {
mRecipes = imgProfile;
final ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Comment");
query.getFirstInBackground(new GetCallback<ParseObject>() {
@Override
public void done(final ParseObject imgProfile, ParseException e) {
if (e == null) {
} else {
imgProfile.getObjectId();
ParseFile fileObject = imgProfile.getParseFile("CommentFile");
fileObject.getDataInBackground(new GetDataCallback() {
public void done(byte[] data, ParseException e) {
if (e == null) {
// Decode the Byte[] into Bitmap
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0,
data.length);
// Get the ImageView from profile.xml
imgPic = (ParseImageView) findViewById(R.id.img_recipes);
// Set the Bitmap into the ImageView
imgPic.setImageBitmap(bmp);
queryAndPopulateMsgs();
}
}
});
}
}
});
} else {
}
}
});
}
}
});
}
};
}
private void refreshMsgs() {
queryAndPopulateMsgs();
}
protected void queryAndPopulateMsgs() {
ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser != null) {
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Comment");
query.orderByDescending("createdAt");
query.setLimit(300);
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> comment, ParseException e) {
if (e == null) {
mRecipes = comment;
RecipesAdapter adapter = new RecipesAdapter(getListView().getContext(), mRecipes);
setListAdapter(adapter);
final ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("ImageProfile");
query.getFirstInBackground(new GetCallback<ParseObject>() {
@Override
public void done(final ParseObject imgProfile, com.parse.ParseException e) {
if (e == null) {
} else {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
}
});
}
}
});
}
}
@Override
protected void onResume() {
super.onResume();
queryAndPopulateMsgs();
}
private void dialogChooseFrom() {
final CharSequence[] items = {"From Gallery", "From Camera"};
android.app.AlertDialog.Builder chooseDialog = new android.app.AlertDialog.Builder(this);
chooseDialog.setTitle("Pick your choice").setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (items[which].equals("From Gallery")) {
Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_LOAD_GALLERY_IMAGE);
} else {
try {
File photoFile = createImageFile();
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(cameraIntent, RESULT_LOAD_CAMERA_IMAGE);
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
chooseDialog.show();
}
private byte[] readInFile(String path) throws IOException {
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();
}
}
Empfohlener redaktioneller Inhalt
Mit Deiner Zustimmung wird hier ein externer Inhalt geladen.
Mit Klick auf den oben stehenden Button erklärst Du Dich damit einverstanden, dass Dir externe Inhalte angezeigt werden dürfen. Dabei können personenbezogene Daten an Drittanbieter übermittelt werden. Mehr Infos dazu findest Du in unserer Datenschutzerklärung.