尝试在Android片段中设置BitMap

时间:2017-08-01 01:09:34

标签: android android-fragments android-fragmentactivity fragmentmanager

我尝试使用我创建的onCaptureImageResult()和onSelectFromGalleryResult()方法在Fragment中设置BitMap。这是代码:

private void onCaptureImageResult(Intent data) {
    Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);

    File destination = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg");

    FileOutputStream fo;
    try {
        destination.createNewFile();
        fo = new FileOutputStream(destination);
        fo.write(bytes.toByteArray());
        fo.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    ivImage.setImageBitmap(thumbnail);
    try {
        ivImage.setImageBitmap(BitmapFactory.decodeStream((InputStream)new URL(data.getData().toString()).getContent()));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private void onSelectFromGalleryResult(Intent data) {
    Bitmap bm=null;
    if (data != null) {
        try {
            bm = MediaStore.Images.Media.getBitmap(getActivity().getApplicationContext().getContentResolver(), data.getData());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //ivImage.setImageBitmap(bm);
    //ivImage.setImageBitmap(BitmapFactory.decodeFile(bm.getPath(data.getData())));
    try {
        ivImage.setImageBitmap(BitmapFactory.decodeStream((InputStream)new URL(data.getData().toString()).getContent()));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_FILE)
            onSelectFromGalleryResult(data);
        else if (requestCode == REQUEST_CAMERA)
            onCaptureImageResult(data);
    }

    // Check which request we're responding to
    System.out.println("Result: " + RESULT_OK + " " + resultCode);
    System.out.println("REQUEST CODE: " + EXIT_DIALOG + " " + requestCode);

    if (requestCode == EXIT_DIALOG) {
        System.out.println("Result: " + RESULT_OK + " " + resultCode);
        if (resultCode == RESULT_OK) {
            getActivity().getSupportFragmentManager().beginTransaction()
                    .replace(R.id.contentLayout, new LiveGameListFragment())
                    .commitAllowingStateLoss();
        }
    }
}
    private void galleryIntent() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);//
    startActivityForResult(Intent.createChooser(intent, "Select File"),SELECT_FILE);
}

private void cameraIntent() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent, REQUEST_CAMERA);
}

最初我使用这行代码将BitMap设置为ImageView:

ivImage.setImageBitmap(bm);

然而,这并没有改变图像。所以我去修改了这个:

ivImage.setImageBitmap(BitmapFactory.decodeFile(bm.getPath(data.getData())));

以下是我在logcat中看到的内容:

07-31 20:42:11.262 31589-31589/com.movetheballsports.login E/ViewRootImpl: sendUserActionEvent() mView == null
07-31 20:42:12.936 20069-20069/? E/ViewRootImpl: sendUserActionEvent() mView == null
07-31 20:42:18.508 3234-3745/? E/NetlinkEvent: NetlinkEvent::FindParam(): Parameter 'LABEL' not found
07-31 20:42:18.508 3234-3745/? E/NetlinkEvent: NetlinkEvent::FindParam(): Parameter 'UID' not found
07-31 20:42:24.591 5310-5478/? E/ContactsProvider_EventLog: Flush buffer to file cnt : 1 size : 0Kb duration : 3ms lastUpdatedAfter : 57978 ms mFlush_time_threasold : 2000 mCurrentSize : 465

想我会得到路径并能够将BitMap设置为ImageView,但是这也没有用。这一切都是在片段中完成的。为什么这不起作用的任何建议?任何帮助将不胜感激!

0 个答案:

没有答案