2

我正在开发用于编辑照片的应用程序

我在第一个活动中有一个按钮,在第二个活动中有一个 ImageView。当我单击该按钮时,它将打开画廊,我将能够选择一个图像。所选图像需要出现在我的第二个活动中的 ImageView 中,但它没有。

下面是我的代码,它不起作用,你知道有什么问题吗?

第一项活动:

package com.example.odabirslike;

import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class Pocetni extends Activity {

    private Button buttonLoadImage;

    private static final int SELECT_PICTURE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pocetni);

        this.buttonLoadImage = (Button) this.findViewById(R.id.pickbutton);

        buttonLoadImage.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View arg0) 
            {
                Intent i = new Intent();
                  i.setType("image/*");
                  i.setAction(Intent.ACTION_GET_CONTENT);
                  startActivityForResult(Intent.createChooser(i, "Complete action using"), SELECT_PICTURE);               
            }
        });
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
         Bitmap selectedphoto   = null;
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == SELECT_PICTURE && 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 filePath = cursor.getString(columnIndex);
            selectedphoto = BitmapFactory.decodeFile(filePath);
            cursor.close();
            Intent i = new Intent (Pocetni.this, Drugi.class);
            i.putExtra("data",selectedphoto);
            startActivity(i); 
        }

    }
}

第二个活动:

package com.example.odabirslike;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.view.Menu;
import android.widget.ImageView;

public class Drugi extends Activity {

    ImageView view = (ImageView) findViewById(R.id.imageView1);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drugi);

        Bitmap selectedphoto  =(Bitmap)this.getIntent().getParcelableExtra("data");
        view.setImageBitmap(selectedphoto);
    }
}

安卓清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.odabirslike"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="15" />


    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.odabirslike.Pocetni"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.odabirslike.Drugi"
            android:label="Drugi" >
        </activity>
    </application>

</manifest>
4

2 回答 2

0

可能是位图太大而无法像那样传递。你为什么不代替,把

 String filePath = cursor.getString(columnIndex);
 i.putExtra("data",filepath);

在你的第一个活动中。在你的第二个

String filepath   =this.getIntent().getStringExtra("data");
Bitmap selectedphoto = BitmapFactory.decodeFile(filePath);

至少对您的应用程序来说应该不那么麻烦

于 2013-05-09T19:05:21.837 回答
0
    public class Drugi extends Activity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_drugi);

            ImageView view = (ImageView) findViewById(R.id.imageView1);

            Bitmap selectedphoto  =(Bitmap)this.getIntent().getParcelableExtra("data");
            view.setImageBitmap(selectedphoto);
        }
   }

试试看。但是,替换通过活动发送数据,而是按照之前的建议发送文件名。

于 2015-12-05T06:09:51.433 回答