当活动重新创建时,毕加索不会加载图片

时间:2014-12-26 11:47:40

标签: android picasso

在我的下面的代码中,我做了一个Intent来加载图片并在SharedPreferences中保存图片的URI。 但是,当我重新启动活动时,毕加索并没有加载图片。 我当然想念一些东西,但我不知道是什么?

我的代码:

package com.kogitos.apicasso;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;


    public class MainActivity extends ActionBarActivity {

        private static String SHARE_NAME = "com.kogitos.apicasso.PICTURE";
        private static int RESULT_LOAD_IMAGE = 1;
        private static String PIC_URI = "pic_uri";
        private SharedPreferences mPreferences;
        private TextView mTextView;
        private ImageView mImageView;
        private Button mButton;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mTextView = (TextView) findViewById(R.id.textView);
            mButton = (Button) findViewById(R.id.button);
            mImageView = (ImageView) findViewById(R.id.imageView);
            mButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    startActivityForResult(i, RESULT_LOAD_IMAGE);
                }
            });
        }

        @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) {
                mPreferences = getSharedPreferences(SHARE_NAME, Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = mPreferences.edit();
                editor.putString(PIC_URI, data.getData().toString());
                Log.i("Trace", "onActivityResult " + data.getData().toString());
                editor.commit();
            }
        }

        @Override
        protected void onResume() {
            super.onResume();

            //Check Level and timer
            mPreferences = getSharedPreferences(SHARE_NAME, Context.MODE_PRIVATE);
            String pathToImage = mPreferences.getString(PIC_URI, "null");
            if ( !pathToImage.equals("null") ) {
                Log.i("Trace", "onResume " + pathToImage);
                loadPicture( Uri.parse(pathToImage) );
            }
        }


        public void loadPicture(Uri pathToImage) {
            Log.i("Trace", " >>> load STRING picture  " + pathToImage);
            Picasso.with(getApplicationContext())
                    .load(pathToImage)
                    .into(mImageView, new Callback() {
                        @Override
                        public void onSuccess() {
                            mTextView.setText("ok ");
                        }

                        @Override
                        public void onError() {
                            mTextView.setText("fail ");
                        }
                    });
        }

    }

Permissions:

   <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.kogitos.apicasso" >
        <uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL" />
        <uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name=".MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>

    </manifest>


Log cat :
12-26 13:38:57.699  21302-21302/com.kogitos.apicasso I/Trace﹕ onActivityResult content://com.google.android.apps.photos.content/0/https%3A%2F%2Flh5.googleusercontent.com%2FX31ovWVVRCbA4LY9ZuLWku63pa2tP3rRwFY0Gc_Q1A8%3Ds0-d
12-26 13:38:57.752  21302-21302/com.kogitos.apicasso I/Trace﹕ onResume content://com.google.android.apps.photos.content/0/https%3A%2F%2Flh5.googleusercontent.com%2FX31ovWVVRCbA4LY9ZuLWku63pa2tP3rRwFY0Gc_Q1A8%3Ds0-d
12-26 13:38:57.752  21302-21302/com.kogitos.apicasso I/Trace﹕ >>> load STRING picture  content://com.google.android.apps.photos.content/0/https%3A%2F%2Flh5.googleusercontent.com%2FX31ovWVVRCbA4LY9ZuLWku63pa2tP3rRwFY0Gc_Q1A8%3Ds0-d

Restart:
12-26 13:40:14.890  21508-21508/com.kogitos.apicasso I/Trace﹕ onResume content://com.google.android.apps.photos.content/0/https%3A%2F%2Flh5.googleusercontent.com%2FX31ovWVVRCbA4LY9ZuLWku63pa2tP3rRwFY0Gc_Q1A8%3Ds0-d
12-26 13:40:14.890  21508-21508/com.kogitos.apicasso I/Trace﹕ >>> load STRING picture  content://com.google.android.apps.photos.content/0/https%3A%2F%2Flh5.googleusercontent.com%2FX31ovWVVRCbA4LY9ZuLWku63pa2tP3rRwFY0Gc_Q1A8%3Ds0-d

0 个答案:

没有答案